1
I’m learning how to use Cakephp, and I’m trying to make a tool to upload images using cake 2.4.4. the problem is that only the fields modified
and created
are entered 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 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
)
)
)
I didn’t understand this part of your question "When trying to save the image, the controller executes the message
INSERT INTO caketest.galleries(modified,created) VALUES ('2014-05-20 14:55:07', '2014-05-20 14:55:07')
"– Erlon Charles
@Erloncharles I edited the question. I was wrong to copy the line, I apologize.
– Jim
What are the columns of your table?
– Erlon Charles
@Erloncharles id, name, size, created and modified.
– Jim