atnatn
atnatn

Reputation: 238

codeigniter image upload successful, but file not uploaded

I have been trying to upload an image using CodeIgniter's File Upload class. The image is used for a user's avatar. Here is a snippet for my code:

//upload file
            if($_FILES['image']['name'] != ''){
                if (!file_exists($dirpath))
                    mkdir($dirpath);

                $this->load->library('upload');
                $config['upload_path'] = $dirpath;
                $config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
                $config['max_size'] = '0';
                $config['max_width'] = '0';
                $config['max_height'] = '0';
                $config['file_name'] = $_FILES['image']['name'];
                $config['overwrite'] = true;
                $config['remove_spaces'] = false;

                $this->upload->initialize($config);
                $data = "";
                $error = "";

                $photo_result = $this->upload->do_upload('image');
                if ($photo_result)
                    if($action == 'update'){
                        foreach (glob($dirpath.'/'.$old_image.'*') as $file)
                            if (file_exists($file))unlink ($file);
                    }
                    if($new_resto->resized == 0){
                        if(resize_image($widths,$theuser))
                            $this->usermodel->update($theuser->id, array('resized'=>1), 'object',$authorization);
                    }
                echo json_encode(array('config'=>$config,'upload_data'=>$this->upload->data()));
                }
            }

The file upload is supposedly successful, because when I print out $this->upload->data(), it shows that the upload data is not empty. The strange thing is, this code works for creating new users, but when I try to update users, the file is not uploaded. I have tried using chmod($dirpath, 0777) before the upload, and chmod($dirpath, 0755) after, still no luck.

Any ideas? Thanks.

Upvotes: 1

Views: 916

Answers (2)

Mudaser Ali
Mudaser Ali

Reputation: 4309

Please update your Upload.php class with previous version of CodeIgniter

Upvotes: 0

atnatn
atnatn

Reputation: 238

My mistake, I assigned $old_image after I updated the data, so $old_image contains the current image name, causing it to be deleted when i call unlink($file).

Problem solved :)

Upvotes: 2

Related Questions