CakePHP: Upload Error

0

I can not move my images, much less detect the error in the code, and no error messages are being displayed on the screen

My Model Photo:

public $validate = array(
        'uploadImg' => array(
                'uploadError' => array(
                    'rule' => 'uploadError',
                    'message' => 'Fail.',
                    'allowEmpty' => TRUE,
                ),
                'mineType' => array(
                    'rule' => array('mineType',array('image/gif','image/png','image/jpg','image/jpeg')),
                    'message' => 'JUST (GIF,PNG e JPG).',
                    'allowEmpty' =>TRUE,
                ),
                'fileSize' => array(
                        'rule' => array(
                            'fileSize', '<=', '1MB'
                        ),
                        'message' => 'Photos < 1MB',
                        'allowEmpty' => TRUE
                    ),
                'processCoverUpload' => array(
                        'rule' => 'processCoverUpload',
                        'message' => 'Cover Image Upload'
                    ),
            ),
    );

public function processCoverUpload($check = array()){
    if(!is_uploaded_file($check['uploadImg']['tmp_name'])){
        return FALSE;
    }
    if(!move_uploaded_file($check['uploadImg']['tmp_name'], WWW_ROT.'img'.DS.'uploads'.DS.$check['uploadImg']['name'])){
        return FALSE;
    }
    $this->data[$this->alias]['uploadImg'] = 'uploads'.DS.$check['uploads']['name'];
        return TRUE;
}

My Controller

public function add(){
        if($this->request->is('post')){
            $this->Photo->create();
            $data = $this->request->data['Photo'];
            $this->Photo->processCoverUpload($data);
            //debug($this->request);
            if(!$data['uploadImg']['name']){
                unset($data['uploadImg']);
            }

            if($this->Photo->save($data)){
                $this->Session->setFlash(_('Imagem salva'));                
                return $this->redirect(array('controller'=> 'home', 'action' => 'index'));
            }else{
                $this->Session->setFlash(_('Não conseguimos salvar'));
            }
        }       
    }

And my form:

<?php  print $this->Form->create('Photo', array('action'=>'add', 'type' => 'file'));  ?>
    <fieldset>      
        <legend>Upload</legend>
        <?php print $this->Form->input('Image:', array('type' => 'file', 'name' => 'uploadImg')); ?>        
    </fieldset>
        <?php print $this->Form->end(_("Save")); ?>

Debugging the array to see if the tmp is coming I did not find anything strange for what it seems to me

'form' => array( 'uploadImg' => array( 
'name' => '1503852_509064475876526_1876445108_n.jpg', 
'type' => 'image/jpeg', 
'tmp_name' => 'C:\Program Files (x86)\xamp\tmp\php5AA.tmp', 
'error' => (int) 0, 'size' => (int) 8411 ) )

If anyone can help me, I can not identify the problem, and I'm learning how to use cake.

@Edit:   Well, some tests I've done, I realize that maybe I make a mistake inside my first IF clause in the processCoverUpload function, it's falling in the return, but since I already had identified what kind of data my array is coming from, how is it? above, 'tmp_name' seems to be right, right?

    
asked by anonymous 17.06.2014 / 15:24

1 answer

1

Hello, I'm not a cakePHP expert, but it could be a typo between: mimeType and mineType, where the correct one would be: mimeType.

Change your $ validate array in Model Photo as follows

public $validate = array(
    'uploadImg' => array(
            'uploadError' => array(
                'rule' => 'uploadError',
                'message' => 'Fail.',
                'allowEmpty' => TRUE,
            ),
            'mimeType' => array(
                'rule' => array('mimeType',array('image/gif','image/png','image/jpg','image/jpeg')),
                'message' => 'JUST (GIF,PNG e JPG).',
                'allowEmpty' =>TRUE,
            ),
            'fileSize' => array(
                    'rule' => array(
                        'fileSize', '<=', '1MB'
                    ),
                    'message' => 'Photos < 1MB',
                    'allowEmpty' => TRUE
                ),
            'processCoverUpload' => array(
                    'rule' => 'processCoverUpload',
                    'message' => 'Cover Image Upload'
                ),
        ),
);
    
17.06.2014 / 17:09