Cake PHP图片上传

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Cake PHP图片上传脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 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')); ?>

我希望这会奏效

脚本宝典总结

以上是脚本宝典为你收集整理的Cake PHP图片上传全部内容,希望文章能够帮你解决Cake PHP图片上传所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。