【问题标题】:How to upload image file using codeigniter?如何使用codeigniter上传图像文件?
【发布时间】:2014-06-11 20:12:11
【问题描述】:

我想在服务器上上传图像文件并想将图像存储在数据库中。

如何将图像文件存储在数据库中并从数据库中获取图像文件。请帮助解决这个问题。

我已经在项目中完成了这段代码.. 但是在服务器上上传图片时找不到upload_path。 在控制器中

 $config['upload_path'] = base_url().'aplication/upload/'; // the uploaded images path
        $config['allowed_types'] = 'jpg|jpeg|png';/*types of image extentions allowed to be uploaded*/
        $config['max_size'] = '3048';// maximum file size that can be uploaded (2MB)
        $this->load->library('upload',$config);

        if ( ! is_dir($config['upload_path']) ) /* this checks to see if the file path is wrong or does not exist.*/
        {
            echo( $config['upload_path']);
            die("THE UPLOAD DIRECTORY DOES NOT EXIST"); // error for invalid file path
        $this->load->library('upload',$config); /* this loads codeigniters file upload library*/
        }


        if ( !$this->upload->do_upload() )
        {
            $error=array('error'=>$this->upload->display_errors());
            //echo "UPLOAD ERROR ! ".$this->upload->display_errors(); //image error
            //  $this->load->view('main_view',$error);
        }
        else
        {
            $file_data=$this->upload->data();
            $data['img']=base_url().'.application/upload/'.$file_data['file_name'];
            echo("Image upload successfully..");
//================================================

            // $this->load->model('insert_model');
            //  $this->insert_model->submit_image();

//==========================================
            //$this->insert_model->insert_images($this->upload->data());
            // $this->load->model('insert_model');
            //$this->insert_model->submit_image();


        }

【问题讨论】:

标签: codeigniter


【解决方案1】:

这很容易。只需在您的控制器中创建一次图像上传功能。这是我写的一个函数:

function image_upload($path, $size, $width, $height, $new_name, $ext)
{
    $config['upload_path'] = $path;
    $config['allowed_types'] = 'jpg|png';
    $config['max_size'] = $size;
    $config['file_name'] = time() . $new_name . "." . $ext;
    $config['max_width'] = $width;
    $config['max_height'] = $height;
    $this->load->library('upload', $config); //Load codeigniter's file upload library
    if (!$this->upload->do_upload())
    {
        return false;
    } else
    {
        return $config['file_name'];
    }
}

现在在你有文件输入字段的表单的动作函数中调用这个函数,如下所示:

if ($_FILES['userfile']['name'] != "")//Check if file was selected by the user
{
    $ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);//Get the extension of file
    $picture_name = $this->image_upload("your/upload/directory/", 10000, 10000, 100000, "test", $ext);
    //Now save this $picture_name in database

}
else
{
    print_r($this->upload->display_errors());//Display errors if file does not get uploaded successfully
}

就是这样。

【讨论】:

    【解决方案2】:

    阅读This,您将了解如何上传文件。

    现在如何将它存储到数据库中。!

    在控制器中使用这个:

    if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
    
            $this->load->view('upload_form', $error);
        }
        else
                    {   
    
                            $img_data = array('upload_data' => $this->upload->data());                          
                            $full_url = "/uploads/".$img_data['upload_data']['file_name'];
                            $reply = $this->model_name->upload_image($full_url);
    
    
                    }
    

    在模型中使用upload_image($full_url) 函数存储图像名称和路径,以便您可以在需要时取回该路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 2014-01-04
      相关资源
      最近更新 更多