2014-03-06 82 views
1

以下验证在我的模型CakePHP的上传图片 - 无法确定MIME类型

'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; 
    } 

正在做我收到的错误是下面的,我也启用了调试2但没有提供进一步的澄清。

无法确定mimetype。

我已经尝试取消注释

延长= php_fileinfo.dll

并重新启动WAMPServer充分,但它仍然没有奏效。

另外,在控制器类我有下面的代码添加

public function add() { 
     if ($this->request->is('post')) { 
      $this->Image->create(); 
         $data = $this->request->data['Image']; 
         if (!$data['image']['name']) 
         { 
          $this->Session->setFlash(__('There was no image provided.')); 
         } 
      if ($this->Image->save($data)) { 
       $this->Session->setFlash(__('The image has been saved.')); 
       return $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The image could not be saved. Please, try again.')); 
      } 
     } 
    } 

添加视图代码

<?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')); ?> 

任何建议,将不胜感激。

回答

0

该关闭的解决方案,不检查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).'; 
} 
1

不知道这是问题的全部,但:

'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的。

+0

真的,我确实有一个错误,但它并没有解决问题sadly.But感谢帮助无论如何 – Enzero

+0

你也可以添加'image/jpg'到允许的文件类型列表。这似乎并不会解决这个问题。 – Kluny

+0

感谢您的建议。 – Enzero

0

我认为这个问题是在这里(我也有类似的问题):

$this->data[$this->alias]['image'] = 'uploads' . DS . $check['image']['name']; 

后,您将上传文件,您将路径信息分配给$this->data[$this->alias]['image'],但CakePHP认为存在包含有关上载文件的所有信息的阵列。这就是整个验证失败的原因。

具体而言,CakePHP会试图寻找在$this->data[$this->alias]['image']tmp_name但它不会找到它后,它会尝试确定的“无” MIME类型,它就会失败。

0

更改文本;延长= php_fileinfo.dll延长= php_fileinfo.dll的php.ini 这对我的作品。希望它能帮助你们。我正在使用xampp。