无法使用$this>请求>数据保存数据



我开始学习如何使用Cakephp,我正在尝试使用Cake 2.4.4制作文件上传工具。问题是只有modifiedcreated字段被保存到DB,虽然namesize也应该保存,我在SO上尝试了几个答案,但ATM没有固定它。DebugKit显示了这个SQL日志:INSERT INTO caketest . galleries ( modified , created ) 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.'
                )
            )
        )
    );
}
<<p> 视图/strong>
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();

调试($ this ->请求->数据)

array(
'Gallery' => array(
    'file' => array(
        'name' => '1604710_722861904399871_963210258_n.jpg',
        'type' => 'image/jpeg',
        'tmp_name' => 'C:wamptmpphp2B49.tmp',
        'error' => (int) 0,
        'size' => (int) 31483
    )
)
)

您的数据格式不正确,无法保存:

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

请试试:

视图:

            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();

控制器:

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

最新更新