jupiter Posted December 2, 2014 Share Posted December 2, 2014 I'm working on a two-file upload form and want to be able to check the mime types of two filenames in a string variable. The code below was working when the variable @resume_path contained just one filename. How can I modify this to accommodate two files? when I echo $resume_path, I get, for example: cover_letter_centerline.doc, cover_letter_ctg.doc Thanks! //DO NOT TRUST $_FILES['upfile']['mime'] VALUE !! //Check MIME Type by yourself. $finfo = new finfo(FILEINFO_MIME_TYPE); if (false === $ext = array_search( $finfo->file($_FILES['resume_path']['tmp_name']), array( 'txt' => 'text/plain', 'doc' => 'application/msword', 'pdf' => 'application/pdf', ), true )) { throw new RuntimeException('Invalid file format.'); } echo "Sorry, invalid file format. Please try again."; echo $ext; Quote Link to comment https://forums.phpfreaks.com/topic/292845-checking-mime-types-of-two-files-in-a-string/ Share on other sites More sharing options...
RMorrison Posted December 2, 2014 Share Posted December 2, 2014 I think the best way to do this would be put the filenames in an array and check each file individually rather than trying to check 2 at a time. Quote Link to comment https://forums.phpfreaks.com/topic/292845-checking-mime-types-of-two-files-in-a-string/#findComment-1498292 Share on other sites More sharing options...
hansford Posted December 3, 2014 Share Posted December 3, 2014 Use a loop to go through the $_FILES array. for($i = 0; $i < count($_FILES['resume_path']['tmp_name']); $i++) { //your code here } Quote Link to comment https://forums.phpfreaks.com/topic/292845-checking-mime-types-of-two-files-in-a-string/#findComment-1498321 Share on other sites More sharing options...
jupiter Posted December 4, 2014 Author Share Posted December 4, 2014 I tried wrapping my code in a for loop as outlined above and I couldn't get it to work. So I'm trying Plan B. $fileTypeExt = pathinfo($path1,PATHINFO_EXTENSION); echo $fileTypeExt . "<br/>"; $fileTypeExt2 = pathinfo($path2,PATHINFO_EXTENSION); echo $fileTypeExt2 . "<br/>"; // Allow certain file formats if($fileTypeExt == "txt" || $fileTypeExt == "doc" || $fileTypeExt == "pdf") { echo "Your file extensions are allowed."; $uploadOk = 1; } else { echo "Sorry, only TXT, PDF, & DOC files are allowed. <br />"; $uploadOk = 0; } // Allow certain file formats if($fileTypeExt2 == "txt" || $fileTypeExt2 == "doc" || $fileTypeExt2 == "pdf") { echo "Your file extensions are allowed."; $uploadOk2 = 1; } else { echo "Sorry, only TXT, PDF, & DOC files are allowed. <br />"; $uploadOk2 = 0; } Plan B works, but I was wondering if there's a more compact way to write this. I frequently get tripped up on how to extend working logic to additional cases. For instance, do I need a $fileTypeExt and $fileTypeExt2 variables? Same with $uploadOk and $uploadOk2? I guess a switch statement would work if I had many cases. Perhaps one answer is to make a giant conditional, e.g., if($fileTypeExt == "txt" || $fileTypeExt == "doc" || $fileTypeExt == "pdf") && ($fileTypeExt2 == "txt" || $fileTypeExt2 == "doc" || $fileTypeExt2 == "pdf")...but I didn't know if that was the best approach. It would remove the need for two status variables ($uploadOk and $uploadOk2). Thanks in advance for your ideas. Quote Link to comment https://forums.phpfreaks.com/topic/292845-checking-mime-types-of-two-files-in-a-string/#findComment-1498506 Share on other sites More sharing options...
mac_gyver Posted December 4, 2014 Share Posted December 4, 2014 your goal should be to NOT repeat code, but to reuse the same, identical code. also, by creating a series of numbered variables, you must now type out or copy/paste/overtype the code for each additional set of data. you should instead use arrays for sets of same meaning data that will be processed in the same way. the looping that hansford showed is for an array of uploaded files. the php.net upload handling documentation shows how to do this. see example #3 at this link - http://php.net/manual/en/features.file-upload.post-method.php as to the validation in the php code, since this is user submitted data, each possible error should be checked separately, for each file, and you should give the visitor as much information as possible about what file and what error occurred with that file. any validation error messages you produce should be added as elements to an array, so that you build a set of all the validation errors. you can then simply loop over this array of error messages at the point where you want to display them. short-answer, use arrays when dealing with sets of data, i.e. sets of input uploaded files, sets of validation error messages, ... Quote Link to comment https://forums.phpfreaks.com/topic/292845-checking-mime-types-of-two-files-in-a-string/#findComment-1498553 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.