在CodeIgniter中更改默认图像上载位置的URL

时间:2012-01-22 12:08:18

标签: codeigniter file-upload codeigniter-url base-path

我们正在使用CodeIgniter框架并有一个上传页面。我们的网站位于http://www.mysite.com/directory1/ base_path ),默认情况下,图片会上传到文件夹http://www.mysite.com/directory1/upload/thumb/,但我们需要将上传的图片发送到{{3 }}

我尝试修改以下行无效

'new_image' => $base_path.'upload/thumb/'.$picture['file_name'],

不成功的修改:

'new_image' => $base_path.'../directory2/upload/thumb/'.$picture['file_name'],

'new_image' =>'http://www.mysite.com/directory2/upload/thumb/'.$picture['file_name'],

有什么想法吗?谢谢!

编辑:为了Romain Guidoux的利益,这里是整个功能:

function add_gallery($id)
    {


        $CI =& get_instance();
        $base_path = $CI->config->slash_item('base_path');


            $project_id=$this->session->userdata('project_id');

            if($_FILES['file_up']['name']!='')
            {

                $cnt=1; 

                $this->load->library('upload');
                $rand=rand(0,100000);

                 for($i=0;$i<count($_FILES['file_up']['name']);$i++)
                 {

                    if($_FILES['file_up']['name'][$i]!='')
                    {

                        $_FILES['userfile']['name']    =   $_FILES['file_up']    ['name'][$i];
                        $_FILES['userfile']['type']    =   $_FILES['file_up']    ['type'][$i];
                        $_FILES['userfile']['tmp_name'] =   $_FILES['file_up']    ['tmp_name'][$i];
                        $_FILES['userfile']['error']       =       $_FILES['file_up']['error'][$i];
                        $_FILES['userfile']['size']    =   $_FILES['file_up']    ['size'][$i]; 



                        $config['file_name']     = $rand.'project_'.$i;
                        $config['upload_path'] = 'upload/orig/';                    
                        $config['allowed_types'] = 'jpg|jpeg|gif|png|bmp';


                       $this->upload->initialize($config);


                            if (!$this->upload->do_upload())
                            {       

                             $error =  $this->upload->display_errors();

                            } 

                                $picture = $this->upload->data();

                                $this->load->library('image_lib');


                                $this->image_lib->clear(); 

                                $this->image_lib->initialize(array(
                                'image_library' => 'gd2',
                                'source_image' =>     $base_path.'upload/orig/'.$picture['file_name'],
                                'new_image' =>     $base_path.'upload/test/'.$picture['file_name'],
                                'maintain_ratio' => TRUE,
                                'quality' => '100%',
                                'width' => 602,
                                'height' => 340
                                ));


                                if(!$this->image_lib->resize()){

                                            $error = $this-    >image_lib->display_errors();

                                }    

1 个答案:

答案 0 :(得分:0)

可能是因为您的base_path变量没有尾部斜杠,请尝试:

'new_image' => $base_path.'/../directory2/upload/thumb/'.$picture['file_name'],

编辑:如果您使用路径来归档unlink等函数,那么您的路径必须是相对的,而不是绝对的! 所以试试这个:

'new_image' => 'directory2/upload/thumb/'.$picture['file_name'],