Codeigniter 2.1.0中的图像上传问题 [英] image uploading issue in codeigniter 2.1.0

查看:80
本文介绍了Codeigniter 2.1.0中的图像上传问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的一个项目使用CI 2.1.0和mysql数据库。我的图片上传方法遇到问题。我要上传的图像应保存在uploads目录中,并创建图像的缩略图版本,并且图像路径应保存在数据库中。
我完成的代码可以正常工作,但是存在一个问题,当我上传图像时,在上传目录中我会获得同一图像的两个副本,而在thumbs目录中我会获得一个上传图像的单个副本。我只希望有一张图像的副本,而不是那两个副本。
这是我的代码->

i am using CI 2.1.0 and mysql database for one of my projects. i am facing a problem with my image uploading method. the image i am uploading should be saved in uploads directory and create a thumbnail version of the image and the image path should be saved in database. the code i have done works fine but there is one problem that when i upload an image, in the upload directory i get two copies of the same image and in the thumbs directory a single copy of the uploaded image. i want to have only one copy of the image instead of those two copies . here is my code->

模型:

function do_upload() //to upload images in upload directory
        {
            $i=$this->db->get('portfolio')->num_rows();
            $i=$i+1;
            $image_path=realpath(APPPATH . '../uploads');
            $config=array(
                'allowed_types'=>'jpeg|png|gif|jpg',
                'upload_path'=>$image_path,
                'max_size'=>2097152,
                'file_name'=>'_'.$i.'_'
            );
            $this->load->library('upload', $config);
            $this->upload->do_upload();
            $image_data = $this->upload->data();
            $config=array(
                'source_image'=>$image_data['full_path'],
                'new_image'=>$image_path.'/thumbs',
                'maintain_ration'=>TRUE,
                'width'=>150,
                'height'=>100
            );
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
            if( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());
                return $error;
            }
            else
            {
                return $image_data;
            }
        }


一些请告诉我为什么要两份的图片正在上传。
还有另一件事,如果存在具有相同名称的图像,我希望图像被覆盖。我已将 system-> libraries 中的 upload.php 文件更改为此

public $overwrite               = TRUE;

,但它不起作用。有人请帮忙。

but it is not working. someone please help.

推荐答案

您两次调用$ this-> upload-> do_upload()..

you are calling $this->upload->do_upload() twice ..

请尝试输入此代码

警告:未经测试

function do_upload() 

        {
            $i=$this->db->get('portfolio')->num_rows();
            $i=$i+1;

            $image_path=realpath(APPPATH . '../uploads');

            $config=array(
                'allowed_types'=>'jpeg|png|gif|jpg',
                'upload_path'=>$image_path,
                'max_size'=>2097152,
                'overwrite'=>TRUE,
                'file_name'=>'_'.$i.'_'
            );


            $this->load->library('upload', $config);


        if( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());
                return $error;
            }
            else
            {

        $image_data = $this->upload->data();
            $config=array(
                'source_image'=>$image_data['full_path'],
                'new_image'=>$image_path.'/thumbs',
                'maintain_ration'=>TRUE,
                'width'=>150,
                'height'=>100
            );
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
                return $image_data;
            }
        }

这篇关于Codeigniter 2.1.0中的图像上传问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆