html - PHP Form Upload - Extension invalid when file left blank -


i have php form allows multiple uploads of accepted extentions:

$allowedextensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt"); 

if extention isn't valid enters this:

if(!in_array($ext,$allowedextensions)) {             die("file $file_name has extensions $ext not allowed");     } 

at moment i'm getting error "file has extensions not allowed" when field left blank. have tried adding array empty" ", blank file's extention be?

what best way around this? load in preset image when not filled in or have solution?

here source code, maybe can else:

<?php   error_reporting(e_all);  ini_set('display_errors', 1);  if(isset($_files) && (bool) $_files) {      $allowedextensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt");      $files = array();     foreach($_files $name=>$file) {         $file_name = $file['name'];          $temp_name = $file['tmp_name'];         $file_type = $file['type'];         $path_parts = pathinfo($file_name);         $ext = $path_parts['extension'];         if(!in_array($ext,$allowedextensions)) {             die("file $file_name has extensions $ext not allowed");          }         array_push($files,$file);     }       // email fields: to, from, subject, , on     $to = "<info@xxx.co.uk>";     $from = "<info@xxx.co.uk>";      $subject ="test attachment";      $message = "this test message";     $headers = "from: $from";      $band = $_post['band'];      // boundary      $semi_rand = md5(time());      $mime_boundary = "==multipart_boundary_x{$semi_rand}x";       // headers attachment      $headers .= "\nmime-version: 1.0\n" . "content-type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";       // multipart boundary      $message = $band;     $message = "this multi-part message in mime format.\n\n" . "--{$mime_boundary}\n" . "content-type: text/plain; charset=\"iso-8859-1\"\n" . "content-transfer-encoding: 7bit\n\n" . $message . "\n\n";      $message .= "--{$mime_boundary}\n";      // preparing attachments     for($x=0;$x<count($files);$x++){         $file = fopen($files[$x]['tmp_name'],"rb");         $data = fread($file,filesize($files[$x]['tmp_name']));         fclose($file);         $data = chunk_split(base64_encode($data));         $name = $files[$x]['name'];         $message .= "content-type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .          "content-disposition: attachment;\n" . " filename=\"$name\"\n" .          "content-transfer-encoding: base64\n\n" . $data . "\n\n";         $message .= "--{$mime_boundary}\n";     }     // send      $ok = mail($to, $subject, $message, $headers);      if ($ok) {      echo "<p>photos sent!</p>";      } else {          echo "<p>mail not sent!</p>";      }  }     ?> 


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -