Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP:保存数据时出错_Php_Cakephp - Fatal编程技术网

CakePHP:保存数据时出错

CakePHP:保存数据时出错,php,cakephp,Php,Cakephp,所以我在用CakePHP保存数据时遇到了问题 如果我上传一个图像(意味着['PictureForm']['file']['name']存在),一切正常,数据被保存。但是,如果['PictureForm']['file']['name']为空,并且['PictureForm']['url']存在,则映像已正确保存在磁盘上,但$this->PictureForm->save($this->data)失败 有人看到我的代码有任何明显的错误吗 if (isset($this->data['Pict

所以我在用CakePHP保存数据时遇到了问题

如果我上传一个图像(意味着['PictureForm']['file']['name']存在),一切正常,数据被保存。但是,如果['PictureForm']['file']['name']为空,并且['PictureForm']['url']存在,则映像已正确保存在磁盘上,但$this->PictureForm->save($this->data)失败

有人看到我的代码有任何明显的错误吗

if (isset($this->data['PictureForm'])) {
                            ///////////////////////////////////////////////////////DEV
                            if(!$this->data['PictureForm']['file']['name']) {
                                    if (!$this->data['PictureForm']['url']) {
                                            $this->Session->setFlash('Error: no image URL or file given!');
                                            $this->redirect(array('action' => '/'));
                                    } else {
                                            $fileExtension = getExtension($this->data['PictureForm']['url']);
                                            $this->request->data['PictureForm']['filename'] = randFilename() . "." . $fileExtension;
                                            file_put_contents('files/picture/' . $this->data['PictureForm']['filename'], file_get_contents($this->data['PictureForm']['url']));
                                    }
                            } else { //file was uploaded
                                    $fileExtension = getExtension($this->data['PictureForm']['file']['name']);
                                    if (!$fileExtension) {
                                            $this->Session->setFlash('Error: no file extension!');
                                            $this->redirect(array('action' => '/'));
                                    }
                                    $this->request->data['PictureForm']['filename'] = randFilename() . "." . $fileExtension;
                                    move_uploaded_file($this->data['PictureForm']['file']['tmp_name'], "files/picture/" . $this->data['PictureForm']['filename']);
                            }
                            $this->request->data = Sanitize::clean($this->request->data, array('encode' => false));
                            if ($this->PictureForm->save($this->data)) {
                                    resizeUpload($this->data['PictureForm']['filename']);
                                    $this->Session->setFlash('Picture <a href="/spootur/media/p/' . $this->data['PictureForm']['filename'] . '">saved</a>');
                                    $this->redirect(array('action' => 'p/' . $this->data['PictureForm']['filename']));
                            } else {
                                    $this->Session->setFlash('Error: could not save to db' . $this->data['PictureForm']['filename']);
                                    $this->redirect(array('action' => '/'));
                            }
                    }
if(isset($this->data['PictureForm'])){
///////////////////////////////////////////////////////发展
如果(!$this->data['PictureForm']['file']['name']){
如果(!$this->data['PictureForm']['url']){
$this->Session->setFlash('错误:没有提供图像URL或文件!');
$this->redirect(数组('action'=>'/);
}否则{
$fileExtension=getExtension($this->data['PictureForm']['url']);
$this->request->data['PictureForm']['filename']=randFilename()。“..$fileExtension;
文件内容($files/picture/.$this->data['PictureForm']['filename'],文件内容($this->data['PictureForm']['url']);
}
}否则{//文件已上载
$fileExtension=getExtension($this->data['PictureForm']['file']['name']);
如果(!$fileExtension){
$this->Session->setFlash('错误:没有文件扩展名!');
$this->redirect(数组('action'=>'/);
}
$this->request->data['PictureForm']['filename']=randFilename()。“..$fileExtension;
移动上传的文件($this->data['PictureForm']['file']['tmp\u name'],“files/picture/”$this->data['PictureForm']['filename']);
}
$this->request->data=Sanitize::clean($this->request->data,array('encode'=>false));
如果($this->PictureForm->save($this->data)){
调整上传大小($this->data['PictureForm']['filename']);
$this->Session->setFlash(“图片”);
$this->redirect(数组('action'=>'p/'.$this->data['PictureForm']['filename']);
}否则{
$this->Session->setFlash('错误:无法保存到数据库。$this->data['PictureForm']['filename']);
$this->redirect(数组('action'=>'/);
}
}

如果保存失败,请在else块中尝试查看验证错误,而不是重定向:

if ($this->PictureForm->save($this->data)) {
    // code
} else{
    debug($this->PictureForm->validationErrors);
}

直接将代码粘贴到问题中,不要链接到网站外。这使得回答起来更容易,也便于以后阅读的人。好的,谢谢。我从来都不知道如何使用debug(),所以这肯定会派上用场。