Olammie Concept
Olammie Concept

Reputation: 1

CodeIgniter not moving image to the destination folder

I am having a problem uploading an image file to a destination folder in my CodeIgniter application and it is not working at all. I have referred so many articles and downloaded some working one but they wouldn't work once in my application and I am thinking it might be my .htaccess file or what.

When I remove the upload $this->upload->do_upload('fileupload') path of the doUpload function and remove the if conditional statement, record got inserted in the database successfully even with extracting the name of the uploaded file and inserting it in the database table but once I put the $this->upload->do_upload('fileupload'), it will not work, meaning the Codeigniter do_upload() is not moving the file.

Please check my code doUpload() function :

public function doUpload()
        {               
                $config['upload_path'] = './Adverts/';  //Adverts is a folder created in the directory where i have codeigniter application folder
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '100048';
                $config['file_name'] = $_FILES['fileupload']['name'];

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

                if ($this->upload->do_upload('fileupload'))
                { 
                    $data2 = array(
                              'AdvertCode'=>$this->input->post('advertcode'),
                              'CustomersName'=>$this->input->post('customersname'),
                              'AdvertSubject'=>$this->input->post('advertsubject'),
                              'AdvertMessage'=>$this->input->post('editor1'),
                              'AdvertPhotoName'=>$config['file_name'],
                              'Addedby'=>$this->input->post('addedby')
                            );

                        $this->db->insert('mytable', $data2);

            $this->session->set_flashdata('Added Successfully','Record  successfully added');
                            $this->loadRecord();                     
                }
                else
                {   
                        echo 'Error uploading file';                                                                                                                           
                }
        }

Below is my .htaccess file :

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

Upvotes: 0

Views: 1274

Answers (3)

rajan snuriya
rajan snuriya

Reputation: 59

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

use this code

Upvotes: 0

Olammie Concept
Olammie Concept

Reputation: 1

I am sorry for my late response due to my health challenge as i fell ill since after i posted this question and i appreciate the help provided by everyone. I have used the dislay_error() to see what the error message for this code is and the error message says : "The upload path does not appear to be valid." .

I have not tested this on the server(hosting server), i worked on localhost and move to server after i have confirmed everything to be working on my localhost. I have changed the upload_path('Adverts/', 'Adverts', './Adverts/', './Adverts') several times but still not moving them to the destination folder.

Below image is my folder structure :

enter image description here

Upvotes: 0

Latief Anwar
Latief Anwar

Reputation: 1868

If you already feel right about your code, try check your directory permission, where you upload files.

may this link help you https://askubuntu.com/questions/303593/how-can-i-chmod-777-all-subfolders-of-var-www

this example for ubuntu user:

root@server1:~# sudo chown www-data:www-data /var/www/html/assets/image
root@server1:~# sudo chmod 775 /var/www/html/assets/image

Upvotes: 1

Related Questions