cakephpfile-uploadwampservercakephp-2.4

Cakephp Upload Image - Can not determine the mimetype


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.


Solution

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