图像无法上传cakephp 2.0
我用了一个组件上传图片,添加组件后控制器没有问题。下面是代码图像无法上传cakephp 2.0,cakephp,Cakephp,我用了一个组件上传图片,添加组件后控制器没有问题。下面是代码 class OesUsersController extends AppController { var $helpers = array('Html', 'Form'); var $components = array('upload'); public function index() { } public function upload() { if (empty($this->data)) {
class OesUsersController extends AppController {
var $helpers = array('Html', 'Form');
var $components = array('upload');
public function index() {
}
public function upload()
{
if (empty($this->data))
{
$this->render();
}
else
{
$this->cleanUpFields();
// set the upload destination folder
$destination = realpath('../../app/webroot/img/uploads/') . '/';
// grab the file
$file = $this->data['Image']['filedata'];
// upload the image using the upload component
$result = $this->Upload->upload($file, $destination, null, array('type' => 'resizecrop', 'size' => array('400', '300'), 'output' => 'jpg'));
if (!$result){
$this->data['Image']['filedata'] = $this->Upload->result;
} else {
// display error
$errors = $this->Upload->errors;
// piece together errors
if(is_array($errors)){ $errors = implode("<br />",$errors); }
$this->Session->setFlash($errors);
$this->redirect('/images/upload');
exit();
}
if ($this->Image->save($this->data)) {
$this->Session->setFlash('Image has been added.');
$this->redirect('/images/index');
} else {
$this->Session->setFlash('Please correct errors below.');
unlink($destination.$this->Upload->result);
}
}
}
这里,数据库字段名:image
控制器名称:OesUsers
型号名称:OesUser
为了完成全部工作,我从这个链接获得了帮助
您的整个表单在add.ctp中是什么样子的 我觉得你没有添加
enctype="multipart/form-data"
到表格。这将导致表单不发布文件
此外,建议使用Form helper创建表单。
使用表单帮助器时,请指定要归档的表单类型
$this->Form->create('model',array('type'=>'file'));
您是否检查过,如果您提交,它将转到上载功能?也许你把表格设置错了。以下是一些你可以阅读的资源。
enctype="multipart/form-data"
$this->Form->create('model',array('type'=>'file'));