如何使用codeigniter上传图像文件?

时间:2014-06-10 05:08:27

标签: codeigniter

我想要在服务器上安装上传图片文件&想将图像存储在数据库中。

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

我在项目中完成了这个代码.. 但是在服务器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();


        }

2 个答案:

答案 0 :(得分:0)

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

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
}

多数民众赞成。

答案 1 :(得分:0)

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

现在如何将其存储到DB中。!

在控制器中使用它:

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)函数存储图像名称和路径,以便您可以在需要时返回该路径。