簡體   English   中英

在cakephp中上傳圖像並將其路徑存儲在數據庫中

[英]Uploading image in cakephp and storing it's path in database

我有一個表單,我試圖上載圖像並將其存儲在數據庫中。 我的控制器代碼是:

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,第1009行]數據庫錯誤錯誤:SQLSTATE [42S22]:找不到列: 1054'字段列表'中的未知列'Array'

在您的控制器中

 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(); ?>

嘗試這個

<?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(); ?>

這個問題應該可以幫助您: cakePHP 3.0上傳圖像

解決方案:這是一種行為,可以幫助您非常輕松地上傳文件!

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

讓我知道你是否在掙扎。

格蕾茲

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM