Cakephp上载图像-无法确定mimetype
以下验证正在我的模型中进行Cakephp上载图像-无法确定mimetype,cakephp,file-upload,wampserver,cakephp-2.4,Cakephp,File Upload,Wampserver,Cakephp 2.4,以下验证正在我的模型中进行 'image' => array( 'uploadErrror' => array( 'rule' => 'uploadError', 'message' => 'The image upload failed.', 'allowEmpty'=> True ),
'image' => array(
'uploadErrror' => array(
'rule' => 'uploadError',
'message' => 'The image upload failed.',
'allowEmpty'=> True
),
'mimeType' => array(
'rule' => array('mimeType',array('image/gif','image/png','image/jpeg')),
'message' => 'Please only upload images (gif, png, jpg).',
'allowEmpty' => true
),
'fileSize'=> array(
'rule' => array('fileSize', '<=', '1MB'),
'message' => 'Image must be less than 1MB.',
'allowEmpty' => true
),
'processImageUpload' => array(
'rule' => 'processImageUpload',
'message' => 'Unable to process image upload.',
'allowEmpty'=> true
) )
public function processImageUpload($check = array()){
if(!is_uploaded_file($check['image']['tmp_name']))
{
return FALSE;
}
$targetdir = WWW_ROOT . 'img' . DS . 'uploads' . DS . $check['image']['name'];
if(!move_uploaded_file($check['image']['tmp_name'],$targetdir))
{
return false;
}
$this->data[$this->alias]['image'] = 'uploads' . DS . $check['image']['name'];
return true;
}
添加视图代码
<?php echo $this->Form->create('Image',array('type'=>'file')); ?>
<fieldset>
<legend><?php echo __('Add Image'); ?></legend>
<?php
echo $this->Form->input('description');
echo $this->Form->input('image',array('type'=>'file'));
echo $this->Form->input('property_id');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
如果您有任何建议,我们将不胜感激。不确定这是否是问题的全部,但:
'mimeType' => array(
'rule' => array('mimeType',array('image/gif','image/png','image/jpeg')),
'message' => 'Please only upload images (gif, png, jpg).',
'alllowEmpty' => true
),
“alllowEmpty”有3个L。关闭的解决方案不检查mime,只检查扩展名
$ext = substr(strtolower(strrchr($check['image']['name'], '.')), 1); //get the extension
$arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions
if (in_array($ext, $arr_ext)) {
//upload code
}
else
{
return 'Please only upload images (gif, png, jpg).';
}
通常,Cake希望在原始文件中指定的位置找到要检查的文件
$this->data[$this->alias]['image']['tmp_name'] // or whatever is the name of the field
在验证过程中。因此,请确保…['tmp\u name']
指向(仍然)现有上载文件的有效位置
原始答复:
我认为问题就在这里(我也有类似的问题):
移动上传的文件后,将路径信息分配给$this->data[$this->alias]['image']
,但CakePHP认为存在一个包含上传文件所有信息的数组。这就是为什么整个验证都会失败
具体来说,CakePHP将尝试在
$this->data[$this->alias]['image']
中查找tmp_name
,但在那里找不到它,之后它将尝试确定mime类型为“nothing”,它将失败。更改文本;extension=php_fileinfo.dll到php.ini上的extension=php_fileinfo.dll
它对我有用。希望对你们有帮助。我使用的是xampp。没错,我确实犯了一个错误,但遗憾的是,它并没有解决问题。但是感谢您的帮助,您也可以将“image/jpg”添加到允许的文件类型列表中。但这似乎也不能解决这个问题。
$this->data[$this->alias]['image']['tmp_name'] // or whatever is the name of the field
$this->data[$this->alias]['image'] = 'uploads' . DS . $check['image']['name'];