Cakephp Upload Image - Can not determine the mimet

2019-08-01 13:37发布

问题:

The following validation is being done in my model

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

The error I am receiving is the below, I also enabled debug 2 but no further clarification was provided.

Can not determine the mimetype.

I have already tried un-commenting

extension=php_fileinfo.dll

and restarted WAMPServer fully but it still did not work.

Also in the controller class I have the following code for add

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

Add View Code

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

Any suggestions would be greatly appreciated.

回答1:

Not sure if this is the whole problem, but:

'mimeType' => array(
             'rule' => array('mimeType',array('image/gif','image/png','image/jpeg')),
             'message' => 'Please only upload images (gif, png, jpg).',
             'alllowEmpty' => true
         ),

'alllowEmpty' has 3 L's.



回答2:

The closes solution which isn't checking mime but just extension is

$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).';
}


回答3:

Change the text ;extension=php_fileinfo.dll into extension=php_fileinfo.dll on php.ini It works for me. Hope it help you guys. I'm using xampp.



回答4:

In general, Cake expects to find the file to check at location specified in original

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

during validation. So make sure the ...['tmp_name'] points to valid location with (still) existing uploaded file.

ORIGINAL ANSWER:

I think the problem is here (I had also similar problem):

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

After you move the uploaded file, you assign path infromation to $this->data[$this->alias]['image'] but CakePHP thinks that there is an array with all information about uploaded file. That is why the whole validation will fail.

To be specific, CakePHP will try to look for tmp_name in $this->data[$this->alias]['image'] but it won't find it there, after that it will try to determine mime type of "nothing" and it will fail.