在cakephp中上传图片并将其存储在数据库

时间:2015-05-26 17:21:57

标签: php cakephp

我有一个表单,我试图上传图像并尝试将其存储在数据库中。 我的控制器代码是:

class OlxProductsController extends AppController{
public function post(){
    if($this->request->is('post')) {
            if ($this->request->data['OlxProduct']['img_upload']) {
                $file = $this->data['OlxProduct']['img_upload']; //put the data into a var for easy use
                //   print_r($file);
                $filename = str_replace(" ", "-", rand(1, 3000) . $file['name']);
                move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/uploads/' . $filename);
                //echo $filename; die();
                $this->request->data['OlxProduct']['img_upload'] = $filename;
            } else {
                unset($this->request->data['OlxProduct']['img_upload']);
            }

            $this->OlxProduct->save($this->request->data);
            $this->Session->setFlash('posted successfully');
            $this->redirect(array('controller' => 'OlxUsers', 'action' => 'account'));
    }
    else {
            $this->Session->setFlash('ad has not been saved');
    }
  }
}

后期行动代码是: //post.ctp

<form action="../post"   method="post" enctype="multipart/form-data" >
<?php
echo $this->Form->input('ad_title');
echo $this->Form->input('category');
echo $this->Form->input('price',array('type'=>'number'));
echo $this->Form->input('ad_description',array('type'=>'textarea',));
echo $this->Form>input('img_upload',array('class'=>'txtbox','type'=>'file'));
echo $this->Form->end('Post Ad');
?>

但是我在执行上面的代码时遇到了这个错误:

  

注意(8):未定义的索引:OlxProduct
  [APP \ Controller \ OlxProductsController.php,第17行]注意(8):数组   字符串转换[CORE \ Cake \ Model \ Datasource \ DboSource.php,line   1009]数据库错误错误:SQLSTATE [42S22]:未找到列:1054   未知列&#39;数组&#39;在&#39;字段列表&#39;

3 个答案:

答案 0 :(得分:8)

在您的控制器中

 if ($this->request->is('post')) 
 {
   $this->OlxProduct>create();
   if(!empty($this->data))
   {
     //Check if image has been uploaded
     if(!empty($this->data['OlxProduct']['image_field_name']['name']))
     {
        $file = $this->data['OlxProduct']['image_field_name']; //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
        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
            if(move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img/upload_folder' . DS . $file['name']))
            {
                //prepare the filename for database entry
                $this->request->data['OlxProduct']['image_field_name'] = $file['name'];
                pr($this->data);
                if ($this->OlxProduct>save($this->request->data)) 
                {
                    $this->Session->setFlash(__('The data has been saved'), 'default',array('class'=>'success'));
                    $this->redirect(array('action'=>'admin_index'));
                }
                else
                {
                    $this->Session->setFlash(__('The data could not be saved. Please, try again.'), 'default',array('class'=>'errors'));
                }
            }
        }
    }
    else
    {
        $this->Session->setFlash(__('The data could not be saved. Please, Choose your image.'), 'default',array('class'=>'errors'));
    }

}
}

在你看来

 <?php echo $this->Form->create('OlxProduct',array('class'=>'form-horizontal','role'=>'form','type'=>'file')); ?>
 <?php echo $this->Form->input("name",array("size"=>"45", 'error' => false,'placeholder'=>'User Name'));?>
 <?php echo $this->Form->input("email",array("size"=>"45", 'error' => false,'placeholder'=>'Email'));?>
 <?php echo $this->Form->input("phone",array("size"=>"45",'error' => false,'placeholder'=>'Phone'));?>
 <?php echo $this->Form->input("image_field_name",array("type"=>"file","size"=>"45", 'error' => false,'placeholder'=>'Upload Image'));?>
 <?php  echo $this->Form->submit('Save', array('name'=>'submit', 'div'=>false)); ?>
 <?php echo $this->Form->end(); ?>

答案 1 :(得分:1)

尝试一下

<?php echo $this->Form->create('OlxProduct',array('class'=>'form-horizontal','role'=>'form','type'=>'file')); ?>
 <?php echo $this->Form->input("name",array("size"=>"45", 'error' => false,'placeholder'=>'User Name'));?>
 <?php echo $this->Form->input("email",array("size"=>"45", 'error' => false,'placeholder'=>'Email'));?>
 <?php echo $this->Form->input("phone",array("size"=>"45",'error' => false,'placeholder'=>'Phone'));?>
 <?php echo $this->Form->input("image_field_name",array("type"=>"file","size"=>"45", 'error' => false,'placeholder'=>'Upload Image'));?>
 <?php  echo $this->Form->submit('Save', array('name'=>'submit', 'div'=>false)); ?>
 <?php echo $this->Form->end(); ?>

答案 2 :(得分:0)

这个问题可以帮助您:cakePHP 3.0 uploading images

解决方案:这是一种帮助您轻松上传文件的行为!

http://cakemanager.org/docs/utils/1.0/behaviors/uploadable/

如果你挣扎,请告诉我。

格尔茨