【问题标题】:Cakephp Upload Image - Can not determine the mimetypeCakephp 上传图片 - 无法确定 mimetype
【发布时间】:2014-04-09 14:31:11
【问题描述】:

正在我的模型中进行以下验证

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

我已经尝试取消注释

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

任何建议将不胜感激。

【问题讨论】:

    标签: cakephp file-upload wampserver cakephp-2.4


    【解决方案1】:

    一般来说,Cake 希望在原始文件中指定的位置找到要检查的文件

    $this->data[$this->alias]['image']['tmp_name'] // or whatever is the name of the field
    

    验证期间。因此,请确保 ...['tmp_name'] 指向有效位置,且上传文件(仍然)存在。

    原始答案:

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

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

    移动上传的文件后,将路径信息分配给$this-&gt;data[$this-&gt;alias]['image'],但CakePHP 认为存在一个包含所有上传文件信息的数组。这就是整个验证失败的原因。

    具体来说,CakePHP 会尝试在$this-&gt;data[$this-&gt;alias]['image'] 中寻找tmp_name,但它不会在那里找到它,之后它会尝试确定“无”的mime 类型,它会失败。

    【讨论】:

      【解决方案2】:

      php.ini 上将文本 ;extension=php_fileinfo.dll 更改为 extension=php_fileinfo.dll 这个对我有用。希望对大家有所帮助。我正在使用 xampp。

      【讨论】:

        【解决方案3】:

        不检查 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).';
        }
        

        【讨论】:

          【解决方案4】:

          不确定这是否是整个问题,但是:

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

          【讨论】:

          • 好吧,我确实有一个错误,但遗憾的是它并没有解决问题。但无论如何感谢您的帮助
          • 您也可以将 'image/jpg' 添加到允许的文件类型列表中。不过,这似乎也不能解决这个问题。
          猜你喜欢
          • 2014-01-13
          • 2016-06-08
          • 2011-10-06
          • 1970-01-01
          • 1970-01-01
          • 2013-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多