脚本宝典收集整理的这篇文章主要介绍了Cake PHP图片上传,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
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) ; } } }
我似乎无法弄清楚为什么上传的图像不会保存到目录中.有人可以请帮助.
以上是脚本宝典为你收集整理的Cake PHP图片上传全部内容,希望文章能够帮你解决Cake PHP图片上传所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。