本文介绍了在cakephp上传图像并将其路径存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a form in which i am trying to upload image and trying to store that in database.My Controller Code is:

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');
    }
  }
}

b $ b //post.ctp

post action code is://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');
?>

但是我执行上面的代码,我得到这个错误:

But I on executing above code, I am getting this error:


推荐答案

b
$ b

In Your Controller

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

这篇关于在cakephp上传图像并将其路径存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 10:41