php - How do I check uploaded file is empty? -


i attempting validation on uploaded images. when check see if images have been selected , uploaded should return error message if no images have been uploaded. in method returns false.

heres method:

class event{          private $dbh;         private $post_data;           public function __construct($post_data, pdo $dbh){                 $this->dbh = $dbh;                 $this->post_data = array_map('trim', $post_data);          }  public function checkvalidimages(){             $errors = array();              if(empty($this->post_data['event-images'])){                 $errors[] = 'please select @ least 1 image upload.';             }              if(count($errors) > 0){                 return $errors;             }else{                 return false;             }          } 

and calling here:

// check images valid         $images = new event($_files, $dbh);         var_dump($imageerrors = $images->checkvalidimages()); 

the var_dump() returns bool(false).

heres form:

<form name="submit-event" action="submit-event.php" method="post" enctype="multipart/form-data"> <div class="large-12 columns no-padding"> <p>select images event</p><br /> <input type="file" class="right" name="event-images[]" size="50" multiple="multiple" /> </div> </form> 

so why method returning false when don't select images.

when html file input left empty, browser still submit name of form element, still entry in $_files array, error code of upload_err_no_file , filename of "".

you should check the error code anyway lots of things can go wrong. validation code becomes like:

$numokayfiles = 0; $numintendedfiles = 0; foreach ($_files['event-images']['error'] $errorcode) {     $numintendedfiles += ($errorcode != upload_err_no_file);     switch ($errorcode) {     case upload_err_ok:         $numokayfiles++;         break;     case upload_err_ini_size:     case upload_err_form_size:         $errors[] = 'your file bigger maximum allowed size.';         break;     case upload_err_no_file:         // ignore         break;     default:         $errors[] = 'a problem occured during file upload.';     } } if ($numintendedfiles == 0) {     $errors[] = 'please select @ least 1 image upload.'; } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -