Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP upload files check loop if all files match criteria
    text
    copied!<p>I have been trying to code a script to upload files only if all selected files match predefined type (pdf,doc or docx). For example if user select 3 files all of them need to match the criteria or none of the selected ones will be uploaded.</p> <p>here is what I have so far:</p> <pre><code>&lt;?php for($i=0; $i&lt;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] &gt; 0) { echo "Return Code: " . $_FILES["file"]["error"][$i] . "&lt;br&gt;"; } 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 one of your files has the wrong extension! you can only select doc,docx or pdf"; } } ?&gt; &lt;form id="form2" name="form2" method="post" action="" enctype="multipart/form-data"&gt; &lt;input name="file[]" type="file" id="file" class="for-text-fields" multiple="multiple"/&gt; &lt;label for="textarea2"&gt;&lt;/label&gt; &lt;textarea name="textarea2" id="textarea2" rows="10" class="for-text-f"&gt;&lt;/textarea&gt; &lt;br /&gt;&lt;br /&gt; &lt;input name="submit" type="submit" id="submit" value="submit" /&gt; &lt;/form&gt; </code></pre> <p>I know this should not be hard to do but I can not sort it out :( I would really appriciate your help on this one.</p> <p>EDIT: I forgot to mention that there will be 4 text fields where users will type some title for each selected file, be it 1, 2,3 or four and thats why I want all of the selected ones to match the type, namely to easily write the update script... hope this makes any sense </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload