Changing URL of default image upload location in CodeIgniter

391 Views Asked by At

We are using the CodeIgniter framework and have an upload page. Our site is at http://www.mysite.com/directory1/ (which is the base_path) and by default the images are uploaded to the folder http://www.mysite.com/directory1/upload/thumb/ but we need our uploaded images to be sent to http://www.mysite.com/directory2/upload/thumb/

I've tried modifying the below line to no effect

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

Unsuccessful modifications:

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

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

Any ideas? Thank you!

EDIT: For the benefit of Romain Guidoux, here is the whole function:

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

There are 1 best solutions below

3
On

It could be because your base_path variable has no trailing slash, so try :

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

EDIT : If you use your path to file functions such as unlink, your path has to be relative, not absolute ! So try this :

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