Reputation: 379
I'm trying to create an upload form with CodeIgniter to give users the possibility to change their profile picture. In my Controller is the following lines of code:
if(!$this->upload->do_upload() && empty($_FILES['userfile'])) {
$data['upload'] = "0";
$this->load->view('editpicture', $data);
} elseif (!$this->upload->do_upload() && !empty($_FILES['userfile'])) {
$data['upload'] = "0";
$data['attempt'] = "1";
$data['error'] = $this->upload->display_errors();
$this->load->view('editpicture', $data);
} else {
$upload_data = $this->upload->data();
....
Everything works fine except when I get an error. The view shows the same error twice:
You did not select a file to upload.
You did not select a file to upload.
Upvotes: 1
Views: 237
Reputation: 15044
Do this:
if(!$this->upload->do_upload()) {
if (empty($_FILES['userfile'])) {
$data['upload'] = "0";
$this->load->view('editpicture', $data);
} else {
$data['upload'] = "0";
$data['attempt'] = "1";
$data['error'] = $this->upload->display_errors();
$this->load->view('editpicture', $data);
}
} else {
$upload_data = $this->upload->data();
....
The reason why it is happening is you are running $this->upload->do_upload()
twice. The first condition runs it to see if it is FALSE
and then checks $_FILES
if it is empty to run the block of code.
Well if $this->upload->do_upload()
is TRUE and $_FILES
is empty then it will run, but if $_FILES
is not empty it will run next condition where you run $this->upload->do_upload()
again in the condition. The function is being executed in the if()
twice then. Conditional checks run left to right.
Upvotes: 1