Sunday, 15 July 2012

php - How to edit my code to stop uploading of allowed file when at least 1 of them is not valid? -


I have the following code that I use to upload two files with two inputs. (No multiple).

The problem is that if 1 file is correct and the other is not, for example one is .jpg and the second is .exe, .jpg is uploaded and. Exe is not

How can I edit my code so if at least 1 of the 2 files are not correct then nothing will be uploaded and the appropriate message will be displayed?

If both files are OK, then proceed with uploads.

Message: 1. Very Large File 2. No Valid Extensions

  $ valid_formats = array ("jpg", "gif", "zip", "bmp" , "Pdf", "doctor", "docs"); $ Max_file_size = 1024 * 300; $ 100 / path $ = "upload /"; // directory $ count = 0 Upload; // To execute all the files, loop $ $ _FILES foreach ($ _FILES ['files'] [' name '] as $ f => $ name) {if ($ _FILES [' files'] [' Size '] [$ f] & gt; $ max_file_size {header ("location: http: // www"); } Elseif (! In_array (pathinfo ($ name, PATHINFO_EXTENSION), $ valid_formats) {header ("location: http: // www"); } Else {// No Errors Found! Move uploaded files $ random = rand (1,30); If (move_uploaded_file ($ _ files ["files"] ["tmp_name"] [$ f], $ path. $ Random. $ Name) {$ count ++; // Header of successfully uploaded files ("Location: http: // www"); }}}}  

You have to do it in a different loop:

  $ valid_formats = array ("jpg", "gif", "zip", "bmp", "pdf", "doc", "docx"); $ Max_file_size = 1024 * 300; $ 100 / path $ = "upload /"; // directory $ count = 0 Upload; Foreach ($ _FILES ['files'] ['name'] $ f = & gt; $ name) {if ($ _FILES ['files'] ['size'] [$ f] & gt; $ max_file_size) {header ("Location: http: // www"); Go out(); } Elseif (! In_array (pathinfo ($ name, PATHINFO_EXTENSION), $ valid_formats) {header ("location: http: // www"); Go out(); }} // loop $ _FILES to execute all the files foreach ($ _FILES ['files'] ['name'] as $ f = & gt; $ name] {$ random = rand (1,30) ; If (move_uploaded_file ($ _ files ["files"] ["tmp_name"] [$ f], $ path. $ Random. $ Name) {$ count ++; // Header of successfully uploaded files ("Location: http: // www"); }}  

No comments:

Post a Comment