Cake PHP图片上传
发布时间:2020-12-13 22:06:29 所属栏目:PHP教程 来源:网络整理
导读:我是 PHP的新手,我需要一些上传图片的帮助.我需要允许用户上传图像,我希望将该图像保存在目录(www / CakePHP / app / webroot / img /)中,并且还希望数据库存储图像的文件路径.这是我到目前为止所得到的, ProductsADD.ctp: div class="products form"?php e
我是
PHP的新手,我需要一些上传图片的帮助.我需要允许用户上传图像,我希望将该图像保存在目录(www / CakePHP / app / webroot / img /)中,并且还希望数据库存储图像的文件路径.这是我到目前为止所得到的,
ProductsADD.ctp: <div class="products form"> <?php echo $this->Form->create('Product'); ?> <fieldset> <legend><?php echo __('Add Product Details'); ?></legend> <?php echo $this->Form->input('product_name',array('required'=>false)); echo $this->Form->input('product_model_number',array('required'=>false)); echo $this->Form->input('product_brand',array('required'=>false)); echo $this->Form->input('product_description',array('required'=>false)); echo $this->Form->input('price_bronze',array('required'=>false)); echo $this->Form->input('price_silver',array('required'=>false)); echo $this->Form->input('price_gold',array('required'=>false)); echo $this->Form->input('upload',array('type'=>'file')); ?> </fieldset> <?php echo $this->Form->end(__('Submit')); ?> </div> ProductsController的: public function add() { if ($this->request->is('post')) { $this->Product->create(); if ($this->Product->save($this->request->data)) { $this->Session->setFlash(__('The product has been saved.')); return $this->redirect(array('action' => 'index')); } else { $this->Session->setFlash(__('The product could not be saved. Please,try again.')); } if(!empty($this->data)) { //Check if image has been uploaded if(!empty($this->data['products']['upload']['name'])) { $file = $this->data['products']['upload']; //put the data into a var for easy use $ext = substr(strtolower(strrchr($file['name'],'.')),1); //get the extension $arr_ext = array('jpg','jpeg','gif'); //set allowed extensions //only process if the extension is valid if(in_array($ext,$arr_ext)) { //do the actual uploading of the file. First arg is the tmp name,second arg is //where we are putting it move_uploaded_file($file['tmp_name'],WWW_ROOT . 'CakePHP/app/webroot/img/' . $file['name']); //prepare the filename for database entry $this->data['products']['product_image'] = $file['name']; } } //now do the save $this->products->save($this->data) ; } } } 我似乎无法弄清楚为什么上传的图像不会保存到目录中.有人可以请帮助. 解决方法
在使用文件输入在cakephp中创建新表单时,您必须设置’enctype’=>’multipart / form-data’
代码应该是这样的: <?php echo $this->Form->create('Product',array('enctype'=>'multipart/form-data')); ?> 我希望这会奏效 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |