Welcome Guest, Not a member yet? Register   Sign In
how to make an image field optional?
#1

[eluser]oliur[/eluser]
I've managed to upload an image successfully and save it's details into the database using the UPLOAD library.

This snippet of code checks if the image upload has been successful:
Code:
if(!$this->upload->do_upload()){
   echo $this->upload->display_errors();
}

If I leave the image field empty it gives an error message : You did not select a file to upload.

But i want to make this image field optional, how do I do that?
#2

[eluser]saidai jagan[/eluser]
If the File field is set then allow the process
Code:
if(isset($_FILE['field_name']))
{
  if(!$this->upload->do_upload())
  {
    echo $this->upload->display_errors();
  }
}
#3

[eluser]Craig A Rodway[/eluser]
You can check the emptiness of PHP's $_FILES array to see if a file was uploaded. Something like...

Code:
if(!empty($_FILES['userfile'])){
    // User uploaded file, do_upload() here...
}

Not 100% sure on that so it would be wise to do a print_r($_FILES); in your code both when you submit a file, and when you leave it empty. You will then know which part of the array to check.
#4

[eluser]oliur[/eluser]
None works for my case. It still gives the same error. Any idea?

Here is the code.
if(isset($_FILES['userfile'])){
if(!$this->upload->do_upload()){
echo $this->upload->display_errors();
}
}else{
/*echo '<pre>';
print_r($this->upload->data());
echo '</pre>';
*/
$upload_data = $this->upload->data();
}
[/code]
#5

[eluser]saidai jagan[/eluser]
if(isset($_FILES['userfile']['file_name']))
#6

[eluser]oliur[/eluser]
YES it did work Smile




Theme © iAndrew 2016 - Forum software by © MyBB