我开始学习如何使用Cakephp,我正在尝试使用Cake 2.4.4制作文件上传工具。问题是只有字段modified
和created
被保存到数据库,虽然name
和size
也应该保存,但我在SO上尝试了几个答案,但ATM没有修复它。 DebugKit显示此SQL日志:INSERT INTO
caketest .
图库(
已修改,
已创建) VALUES ('2014-05-20 14:55:07', '2014-05-20 14:55:07')
。此外,在尝试保存时,我总是收到O Ficheiro foi guardado com sucesso.
消息,该消息应在保存完成时显示,并且文件未保存在目标文件夹中。我做错了什么?
控制器
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'));
}
}
}
模型
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.'
)
)
)
);
}
查看
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();
调试($这 - &GT;请求 - &GT;数据)
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
)
)
)
答案 0 :(得分:1)
您的数据格式不正确无法保存:
array('Gallery' => array(
'name' => '1604710_722861904399871_963210258_n.jpg',
'size' => (int) 31483
));
答案 1 :(得分:0)
请试试这个:
查看:
echo $this->Session->flash();
echo "<br>";
echo $this->Form->create('Gallery',array('enctype'=>'multipart/form-data'));
echo "<h3><small>Seleccione uma imagem por favor.</small></h3>";
echo $this->Form->input('file', array('type' => 'file'));//file('file');
echo $this->Form->submit(__('Guardar'), array('class' => 'btn btn-success','formnovalidate' => true)) ;
echo $this->Form->end();
CONTROLLER:
$file = $this->request->data['Gallery']['file'];
if ($file['error'] === UPLOAD_ERR_OK) {
$id = String::uuid();
$file_name_arr = explode(".", $file['name']);
$file_name = str_replace(" ","", $file_name_arr[0]) . $id . "." . end($file_name_arr);
if (move_uploaded_file($file['tmp_name'], APP . 'webroot' . DS . 'uploads' . DS . $file_name)) {
$this->request->data['Gallery']['file'] = $this->BASEURL.'uploads/'.$img_name;
}
}else{
$this->Session->setFlash(__('The file could not be saved. Please, try again.'));
return $this->redirect(array('action' => 'index'));
}
$this->Gallery->create();
if ($this->Gallery->save($this->request->data)) {
$this->Session->setFlash(__('The file has been saved'), 'flash_custom_success');
return $this->redirect(array('action' => 'index'));
} else
$this->Session->setFlash(__('The file could not be saved. Please, try again.'));