PHP upload files check loop if all files match criteria -


i have been trying code script upload files if selected files match predefined type (pdf,doc or docx). example if user select 3 files of them need match criteria or none of selected ones uploaded.

here have far:

<?php  for($i=0; $i<count($_files['file']['name']); $i++) { $file_type = $_files['file']['type'][$i];    $allowedexts = array("application/pdf", "application/msword",  "application/vnd.openxmlformats-officedocument.wordprocessingml.document");  if(in_array($file_type, $allowedexts))  {    if ($_files["file"]["error"][$i] > 0) { echo "return code: " . $_files["file"]["error"][$i] . "<br>"; } else { if (file_exists("uuuuu/" . $_files["file"]["name"][$i]))   {    $file_name = $http_post_files['file']['name'][$i];   $random_digit=rand(0000,9999);    $new_file_name=$random_digit.$file_name;   $file_final = str_replace(' ', '_', $new_file_name);   $path= "uuuuu/".$file_final;    copy($http_post_files['file']['tmp_name'][$i], $path);   $message = "success....";    } else   {   move_uploaded_file($_files["file"]["tmp_name"][$i],   "uuuuu/" . $_files["file"]["name"][$i]);   $message = "success....";    } }   }    else   {      $message1 ="at least 1 of files has wrong extension! can select doc,docx or pdf";    }    }  ?>  <form id="form2" name="form2" method="post" action="" enctype="multipart/form-data"> <input name="file[]" type="file" id="file" class="for-text-fields" multiple="multiple"/>   <label for="textarea2"></label>   <textarea name="textarea2" id="textarea2" rows="10" class="for-text-f"></textarea>   <br /><br />   <input name="submit" type="submit" id="submit" value="submit" /> </form> 

i know should not hard can not sort out :( appriciate on one.

edit: forgot mention there 4 text fields users type title each selected file, 1, 2,3 or 4 , thats why want of selected ones match type, namely write update script... hope makes sense

ok how it, haven't tested it:

edit: try this

//list acceptable types $allowedexts = array("application/pdf", "application/msword",     "application/vnd.openxmlformats-officedocument.wordprocessingml.document");  //loop through uploaded files for($i=0; $i<count($_files['file']['name']); $i++) {      $file_type = $_files['file']['type'][$i];         if(in_array($file_type, $allowedexts)) {         if ($_files["file"]["error"][$i] > 0) {           $errors[] = "return code: " . $_files["file"]["error"][$i] . "<br>";         }else {             //if duplicate file add array change name             if (file_exists("uuuuu/" . $_files["file"]["name"][$i])) {                     $duplicates[$i]['name'] = $_files['file']['name'][$i];                 $duplicates[$i]['tmp_name'] = $_files['file']['tmp_name'][$i];                      }else {                   //it's correct file format, file same name doesn't exist & there's no file error.                    $eligible[$i]['tmp_name'] = $_files["file"]["tmp_name"][$i];                    $eligible[$i]['name']     = $_files['file']['name'][$i];                }         }      }else{         $errors[] = 'incorrect file format '.$_files['file']['name'][$i];     } }  if($duplicates) {      array_filter($duplicates); }  //we have array of possible errors / files ok need name changing / elligible uploads   //first check errors - include incorrect file types state don't want  if($errors) {     die( print_r($errors) ); //could make nicer  }else {   //no errors process file name changes if upload files      if($duplicates) {         for($i=0;$i<count($duplicates);$i++) {              $file_name = $duplicates[$i]['name'];                  $random_digit=rand(0000,9999);              $new_file_name=$random_digit.$file_name;              $file_final = str_replace(' ', '_', $new_file_name);              $path= "uuuuu/".$file_final;              copy($duplicates[$i]['tmp_name'], $path);              $message = "success....";           }     }     //upload remaining elibigle      for($i=0;$i<count($eligible);$i++) {             move_uploaded_file($eligible[$i]['tmp_name'], "uuuuu/" .         $eligible[$i]['name']);     }  } 

new edit: works me..

     if($_files) {          //list acceptable types         $allowedexts = array("application/pdf", "application/msword",     "application/vnd.openxmlformats-officedocument.wordprocessingml.document");          //loop through uploaded files         for($i=0; $i<count($_files['file']['name']); $i++) {              $file_type = $_files['file']['type'][$i];                 if(in_array($file_type, $allowedexts)) {                 if ($_files["file"]["error"][$i] > 0) {                  $errors[] = "return code: " . $_files["file"]["error"][$i] . "<br>";                 }else {                     //if duplicate file add array change name                     if (file_exists("uuuuu/" . $_files["file"]["name"][$i])) {                             $duplicates[$i]['name'] = $_files['file']['name'][$i];                         $duplicates[$i]['tmp_name'] = $_files['file']['tmp_name'][$i];                              }else {                           //it's correct file format, file same name doesn't exist & there's no file error.                            $eligible[$i]['tmp_name'] = $_files["file"]["tmp_name"][$i];                            $eligible[$i]['name']     = $_files['file']['name'][$i];                        }                 }              }else{                 $errors[] = 'incorrect file format '.$_files['file']['name'][$i];             }         }          if($duplicates) {             array_filter($duplicates);         }          //we have array of possible errors / files ok need name changing / elligible uploads           //first check errors - include incorrect file types state don't want          if($errors) {             die( print_r($errors) ); //could make nicer          }else {           //no errors process file name changes if upload files              if($duplicates) {                 for($i=0;$i<count($duplicates);$i++) {                      $file_name = $duplicates[$i]['name'];                     $random_digit=rand(0000,9999);                      $new_file_name=$random_digit.$file_name;                      $file_final = str_replace(' ', '_', $new_file_name);                      $path= "uuuuu/".$file_final;                      move_uploaded_file($duplicates[$i]['tmp_name'], "uuuuu/" .         $new_file_name);                 }             }             //upload remaining elibigle              for($i=0;$i<count($eligible);$i++) {                     move_uploaded_file($eligible[$i]['tmp_name'], "uuuuu/" .         $eligible[$i]['name']);             }          }     } 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -