文件上载在cakePHP 2.x上不起作用
我正在尝试上传用户图片,但在下面的示例中,没有任何内容被保存到数据库中,也没有给出任何错误。我知道必须进行验证,一旦我得到要存储的文件,验证就会完成 以下是视图文件中的代码段:文件上载在cakePHP 2.x上不起作用,cakephp,Cakephp,我正在尝试上传用户图片,但在下面的示例中,没有任何内容被保存到数据库中,也没有给出任何错误。我知道必须进行验证,一旦我得到要存储的文件,验证就会完成 以下是视图文件中的代码段: <?php echo $this->Form->create('User', array('enctype' => 'multipart/form-data')); echo $this->form->input('upload', array('type' => 'file'
<?php
echo $this->Form->create('User', array('enctype' => 'multipart/form-data'));
echo $this->form->input('upload', array('type' => 'file'));
echo $this->Form->end('Submit');
?>
检查以下链接:
检查以下链接:
像这样改变你的观点
<?php echo $this->Form->file('Document.submittedfile'); ?>
public function fileupload() {
if ($this->request->is('post') || $this->request->is('put')) {
//die();
$file = $this->request->data['Document']['submittedfile'];
//$this->pdfadd1->save($this->request->data);
move_uploaded_file($this->data['Document']['submittedfile']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/cakephp3/cakephp1/cakephp/app/webroot/files/' . $this->data['Document']['submittedfile']['name']);
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('Thanks for the submission'));
return $this->redirect(array('controller' => 'users','action' => 'index'));
}
}
请勿忘记在webroot或任何其他位置(用于上传文件)创建文件夹。像这样更改视图
<?php echo $this->Form->file('Document.submittedfile'); ?>
public function fileupload() {
if ($this->request->is('post') || $this->request->is('put')) {
//die();
$file = $this->request->data['Document']['submittedfile'];
//$this->pdfadd1->save($this->request->data);
move_uploaded_file($this->data['Document']['submittedfile']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/cakephp3/cakephp1/cakephp/app/webroot/files/' . $this->data['Document']['submittedfile']['name']);
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('Thanks for the submission'));
return $this->redirect(array('controller' => 'users','action' => 'index'));
}
}
dnt别忘了在webroot或任何其他地方创建文件夹(用于上传文件)这是一个太老的示例,我假设cake php现在处理上传文件的方式不同,因为找不到任何$file数组变量。我看不出我的代码有什么问题。这是一个太老的例子,我假设cake php现在处理上传的文件的方式不同,因为找不到任何$file数组变量。我看不出我的代码有什么问题。它是一种魅力。谢谢还有一个离题的问题。如何在保存之前调试请求->数据数组?在控制器中尝试了调试和var_dump,只是跳过了它并调用了save()。可能是,验证返回false。。您可以像$this->Model->save这样检查($this->data,false);输入false并检查现在是否插入数据,如果是,则验证错误作为一个符咒。谢谢还有一个离题的问题。如何在保存之前调试请求->数据数组?在控制器中尝试了调试和var_dump,只是跳过了它并调用了save()。可能是,验证返回false。。您可以像$this->Model->save这样检查($this->data,false);放置false并检查现在是否插入数据,如果是,则是验证错误