Cake PHP图片上传

时间:2014-04-29 00:40:24

标签: image cakephp image-uploading

我是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) ;
                }
            }

        }

我似乎无法弄清楚为什么上传的图片没有保存到目录中。有人可以帮忙。

5 个答案:

答案 0 :(得分:5)

在带文件输入的cakephp中创建新表单时,您必须设置&#39; enctype&#39; =&gt;&#39; multipart / form-data&#39;

代码应该是这样的:

<?php echo $this->Form->create('Product',array('enctype'=>'multipart/form-data')); ?>

我希望这会有效

答案 1 :(得分:3)

您错过了表单中的多部分,而是:

<?php echo $this->Form->create('Product'); ?>

试试

<?php echo $this->Form->create('Product', array('type' => 'file); ?>

答案 2 :(得分:3)

更改此

<?php echo $this->Form->create('Product'); ?>

<?php $this->Form->create('Product',array('type'=>'file')); ?>

答案 3 :(得分:1)

这是我用于cakephp 2.7.2

的内容
public function add() {
    if ($this->request->is('post')) {
        $this->Category->create();
        //Check if image has been uploaded
        if (!empty($this->request->data['Category']['upload']['name'])) {
            $file = $this->request->data['Category']['upload'];

            $ext = substr(strtolower(strrchr($file['name'], '.')), 1);
            $arr_ext = array('jpg', 'jpeg', 'gif','png');

            if (in_array($ext, $arr_ext)) {
                move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/webimages/categories/' . $file['name']);
                //prepare the filename for database entry
                $this->request->data['Category']['image'] = $file['name'];
            }
        }

        if ($this->Category->save($this->request->data)) {
            $this->Session->setFlash(__('The category has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The category could not be saved. Please, try again.'));
        }
    }
    $parentCategories = $this->Category->ParentCategory->find('list');
    $categoriesStatus = $this->Category->getCategoriesStatus();//model's method to get list of status
    $this->set(compact('parentCategories', 'categoriesStatus'));
}

注意:db中的实际字段是图像,但是我将其更改为上传然后在控制器中我将上传文件名添加到图像中,然后保存在db中。

答案 4 :(得分:0)

这是对我有用的

     public function add() {
    if ($this->request->is('post')) {

      //Check if image was sent
      if(!empty($this->request->data['Post']['upload']))
      {
        $file = $this->request->data['Post']['upload']; // Creating a variable to handle upload
        debug($file);
        $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
        $arr_ext = array('jpg', 'jpeg', 'gif', 'png'); //processing file extension

        //if 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 . 'img/uploads/posts/' . $file['name']);

          //saving file on database
          $this->request->data['Post']['upload'] = $file['name'];
        }
      }

      //now do the save
      if ($this->Post->save($this->request->data)) {
        $this->Flash->success('Your post has been saved with image.');
        $this->redirect(array('action' => 'index'));
      }else {

      }
    }
  }