Welcome Guest, Not a member yet? Register   Sign In
Which is the best way to check if there is file to upload?
#1

[eluser]Skuja[/eluser]
Hi, i have submit form with text and file input fields. When data is submited, i want to check if there is file selected to upload. Currently i am using such check:
Code:
if ( !empty($_FILES['attachment']['name']) && !$this->upload->do_upload('attachment') )  
{
    $data['error'] = array('error' => $this->upload->display_errors());
}

Is there any best practice to do this ?
#2

[eluser]xwero[/eluser]
i use
Code:
if($_FILES['attachment']['error'] != 4)
{
   if(!$this->upload->do_upload('attachment'))
   {
      $data['error'] = array('error' => $this->upload->display_errors());
   }
}
for files that aren't required. Errornumber 4 tell you that no file is selected.
#3

[eluser]Skuja[/eluser]
thanks very much xwero for quick reply, it was just that i needed.
#4

[eluser]MadZad[/eluser]
Indeed, thanks xwero. I shall be utilizing that as well to improve what we've got.

Additionally, I tend to have paranoid code. In this case, I'm trying to validate file size while avoiding "yo dummy, there's no array there" PHP errors:
Code:
if (! (isset($_FILES) and
       array_key_exists('datafile', $_FILES) and
       array_key_exists('size', $_FILES['datafile'])) ) {
  $err_msg = "No data file specified";
}
else if ($_FILES['datafile']['size'] > $this->data['max_file_size']) {
  $err_msg = "Specified file is too large";
}




Theme © iAndrew 2016 - Forum software by © MyBB