Error saving data

1

I'm learning how to use Cakephp, and I'm trying to make a tool for uploading images using Cake 2.4.4. the problem is that only the modified and created fields are inserted into the database, although name and size should also be saved. DebugKit shows this SQL log

INSERT INTO caketest.galleries(modified,created) VALUES ('2014-05-20 14:55:07', '2014-05-20 14:55:07')

When trying to save the image, controller executes the message

$this->Session->setFlash('O Ficheiro foi guardado com sucesso.', 'default', array('class'=>'alert flashMessageSuccess'));'

Always, however, the file is not moved to the server folder. What is the problem with my code?

Controller

public function uploadImages(){
        $this->set('title_for_layout', 'Inserir imagens');
        $this->layout = 'admin';
        if($this->request->is('post') || $this->request->is('put')){

            $this->loadModel('Gallery');
            $file = array(
                'Gallery' => array(
                    $this->request->data['Gallery']
                    )
                );
            $this->Gallery->create();
            debug($this->request->data);
            debug($this->request->data['Gallery']);
            debug($file);
            if($this->Gallery->save($this->request->data)){
                move_uploaded_file($this->data['Gallery']['tmp_name'], WWW_ROOT. 'img/Gallery/' . $this->data['Gallery']['name']);
                $this->Session->setFlash('O Ficheiro foi guardado com sucesso.', 'default', array('class'=>'alert flashMessageSuccess'));
            }else{
                $this->Session->setFlash('Erro ao guardar o ficheiro.', 'default', array('class'=>'alert flashMessageDanger'));
            }
        }
    }

Model

App::uses('AppModel', 'Model');
class Gallery extends AppModel{
    public $useTable = 'galleries';
    var $validate = array(
        'name' => array(
            'is_valid' => array(
                'rule' => 'notEmpty',
                'message' => 'Seleccione um ficheiro por favor.'
                ),
            'is_unique' => array(
                'rule' => 'isUnique',
                'message' => 'Já existe um ficheiro com este nome.'
                ),
            'extension' => array(
                'rule' => array('extension', array('gif', 'jpeg', 'png', 'jpg')),
                'message' => 'O ficheiro deve estar num formato gif, jpeg, jpg ou png.'
            ),
        'size' => array(
            'sizeCheck' => array(
                'rule' => array('fileSize', '<=', '2MB'),
                'message' => 'O ficheiro deve ter um tamanho inferior a 2MB.'
                )
            )

        )
    );
}

View

echo $this->Session->flash();

echo "<br>";
echo $this->Form->create('Gallery',array('type'=>'file'));

echo "<h3><small>Seleccione uma imagem por favor.</small></h3>";
echo $this->Form->input('file', array('type' => 'file'));//file('file');
echo $this->Form->error('file', array(), array('class' => 'alert flashMessageWarning'));
echo "<br>";
echo $this->Form->submit(__('Guardar'), array('class' => 'btn btn-success','formnovalidate' => true)) ;
echo $this->Form->end();

My table has the columns:

 id | name | size | created | modified

Debug ($ this-> request-> data)

array(
    'Gallery' => array(
        'file' => array(
            'name' => '1604710_722861904399871_963210258_n.jpg',
            'type' => 'image/jpeg',
            'tmp_name' => 'C:\wamp\tmp\php2B49.tmp',
            'error' => (int) 0,
            'size' => (int) 31483
        )
    )
)
    
asked by anonymous 20.05.2014 / 19:00

1 answer

0

According to the CookBook your data needs to be as follows to be saved

Array 
(
    [ModelName] => Array
    (
        [fieldname1] => 'value'
        [fieldname2] => 'value'
    )
)

The problem is with the array where the data is to be saved, it saves the image, but saves only the data that is saved without having to pass the values that are < > created and modified

Solution

I'd recommend doing a formatting in your array , uploading the key data " file " to the same level as the key itself or returning an array > like this:

Array(
    'Gallery' => Array(
        'name' => '1604710_722861904399871_963210258_n.jpg',
        'size' => (int) 31483
    )
)

To do this your controller should look like this:

public function uploadImages(){
    $this->set('title_for_layout', 'Inserir imagens');
    $this->layout = 'admin';
    if($this->request->is('post') || $this->request->is('put')){

        $this->loadModel('Gallery');
        $file = array(
            "Gallery" => array(
                "name" => $this->request->data["Gallery"]["file"]["name"],
                "size" => $this->request->data["Gallery"]["file"]["size"]
                )
            );
        $this->Gallery->create();
        if($this->Gallery->save($file)){
            move_uploaded_file($this->data['Gallery']['tmp_name'], WWW_ROOT. 'img/Gallery/' . $this->data['Gallery']['name']);
            $this->Session->setFlash('O Ficheiro foi guardado com sucesso.', 'default', array('class'=>'alert flashMessageSuccess'));
        }else{
            $this->Session->setFlash('Erro ao guardar o ficheiro.', 'default', array('class'=>'alert flashMessageDanger'));
        }
    }
}

Move the file

Regarding moving the file to the move-uploaded-file () says:

  

If filename is not a valid uploaded file, then no action will occur and move_uploaded_file() will return FALSE .

     

If filename is a valid uploaded file, but can not be moved for any reason, no action will occur and move_uploaded_file() will return FALSE . In addition, an alert will be issued.

Therefore write permissions on the target folder, the source folder access to the target folder existence, and anything else that precludes the move_uploaded_file() from moving the file must be checked.

    
20.05.2014 / 19:30