herghost Posted January 16, 2012 Share Posted January 16, 2012 Hi all I am having a problem with an error message firing all the time? if ((($_FILES["file"]["type"] != "application/zip")) || (($_FILES["file"]["type"] != "application/x-zip-compressed")) || (($_FILES["file"]["type"] != "application/x-zip"))) { header('Location: scontrol.php?alert=notzip'); } This is firing when I try uploading a zip, I have 2 questions, have I missed a MIME type (using Chrome) and is a Winrar ZIP different from a standard zip (its not a .rar file!) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/ Share on other sites More sharing options...
scootstah Posted January 16, 2012 Share Posted January 16, 2012 You have a flaw in your logic. You should be using AND not OR. if ($_FILES['file']['type'] != 'application/zip' && $_FILES['file']['type'] != 'application/x-zip-compressed' && $_FILES['file']['type'] != 'application/x-zip') { header('location: scontrol.php?alert=notzip'); } This way, if $_FILES['file']['type'] is one of the three the IF will fail, as expected. Quote Link to comment https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308289 Share on other sites More sharing options...
herghost Posted January 16, 2012 Author Share Posted January 16, 2012 Ehm, Im not sure on that logic as it still fires the error? I think my logic is correct if is not or is not or is not then error yours would have to meet all 3 conditions? Edit! Actually, yours would be if file is not and is not and is not = error Either way should work unless im mistaken? However neither does Quote Link to comment https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308290 Share on other sites More sharing options...
QuickOldCar Posted January 16, 2012 Share Posted January 16, 2012 if ($_FILES['file']['type'] == 'application/zip' || $_FILES['file']['type'] == 'application/x-zip-compressed' || $_FILES['file']['type'] == 'application/x-zip') { header('location: scontrol.php?alert=notzip'); } Quote Link to comment https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308298 Share on other sites More sharing options...
herghost Posted January 16, 2012 Author Share Posted January 16, 2012 if ((($_FILES["file"]["type"] == "application/zip")) || (($_FILES["file"]["type"] == "application/x-zip-compressed")) || (($_FILES["file"]["type"] == "application/x-zip"))) { header('Location: scontrol.php?alert=notzip'); } Wouldn't this fire the reload if the file is a zip? Quote Link to comment https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308299 Share on other sites More sharing options...
PFMaBiSmAd Posted January 16, 2012 Share Posted January 16, 2012 You need to actually find out what the ['type'] element is. Also, have you checked for upload errors prior to that point, because if the file didn't upload properly, the ['type'] element is probably empty. For debugging purposes only, add the following code to your form processing page, immediately after the first opening <?php tag - echo "<pre>",print_r($_FILES,true),"</pre>"; exit; Quote Link to comment https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308300 Share on other sites More sharing options...
herghost Posted January 16, 2012 Author Share Posted January 16, 2012 Thanks PFM! Array ( [uploadedfile] => Array ( [name] => 629.zip [type] => application/octet-stream [tmp_name] => C:\wamp\tmp\phpB10A.tmp [error] => 0 [size] => 837990 ) ) An Octet-stream? Never heard of them! EDIT --- An ahhhhh - moment php.ini - extension=php_fileinfo.dll EDIT 2 --- Still reads as octet-stream hmm, a bit more reading needed Quote Link to comment https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308302 Share on other sites More sharing options...
QuickOldCar Posted January 16, 2012 Share Posted January 16, 2012 From what I see, you are trying to detect if any compressed type files, like zip or rar, and if it is, redirect them to a page telling them no zip files. if ($_FILES['file']['type'] == "application/zip" || $_FILES['file']['type'] == "application/octet-stream" || $_FILES['file']['type'] == "application/rar" || $_FILES['file']['type'] == "application/x-rar-compressed" || $_FILES['file']['type'] == "application/x-compressed") { header('Location: scontrol.php?alert=notzip'); } You might consider doing an extension check, Quote Link to comment https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308305 Share on other sites More sharing options...
herghost Posted January 16, 2012 Author Share Posted January 16, 2012 From what I see, you are trying to detect if any compressed type files, like zip or rar, and if it is, redirect them to a page telling them no zip files. if ($_FILES['file']['type'] == "application/zip" || $_FILES['file']['type'] == "application/octet-stream" || $_FILES['file']['type'] == "application/rar" || $_FILES['file']['type'] == "application/x-rar-compressed" || $_FILES['file']['type'] == "application/x-compressed") { header('Location: scontrol.php?alert=notzip'); } Wrong way round, trying to redirect if it is not a zip You might consider doing an extension check, Quote Link to comment https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308306 Share on other sites More sharing options...
QuickOldCar Posted January 16, 2012 Share Posted January 16, 2012 ahh, i see so the != is the way Here's how I handle them, I use allowed arrays, if is the array allow it. Is an example upload showing for types and extensions <html> <body> <form action="" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> <?php if(isset($_POST['submit']) && !empty($_FILES["file"]["name"])) { $timestamp = time(); $target = "upload/"; $target = $target . basename($_FILES['uploaded']['name']) ; $ok=1; $allowed_types = array("image/gif","image/jpeg","image/pjpeg","image/png","image/bmp"); $allowed_extensions = array("gif","png","jpg","bmp"); if ($_FILES['file']['size'] > 350000) { $max_size = round(350000 / 1024); echo "Your file is too large. Maximum $max_size Kb is allowed. <br>"; $ok=0; } if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; $ok=0; } else { $path_parts = pathinfo(strtolower($_FILES["file"]["name"])); if(in_array($_FILES["file"]["type"],$allowed_types) && in_array($path_parts["extension"],$allowed_extensions)){ $filename = $timestamp."-".$_FILES["file"]["name"]; echo "Name: " . $filename . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; $path_parts = pathinfo($_FILES["file"]["name"]); echo "Extension: " . $path_parts["extension"] . "<br />"; echo "Size: " . round($_FILES["file"]["size"] / 1024) . " Kb<br />"; //echo "Stored in: " . $_FILES["file"]["tmp_name"]. " <br />"; } else { echo "Type " . $_FILES["file"]["type"] . " with extension " . $path_parts["extension"] . " not allowed <br />"; $ok=0; } } if($ok == 1){ @move_uploaded_file($_FILES["file"]["tmp_name"], $target . $filename); $file_location = $target . $filename; if(file_exists($file_location)){ echo "Uploaded to <a href='$file_location'>$filename</a> <br />"; } else { echo "There was a problem saving the file. <br />"; } } } else { echo "Select your file to upload."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308307 Share on other sites More sharing options...
herghost Posted January 16, 2012 Author Share Posted January 16, 2012 Thanks, ill adapt and test yours Quote Link to comment https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308308 Share on other sites More sharing options...
PFMaBiSmAd Posted January 16, 2012 Share Posted January 16, 2012 Please everyone, check that your file(s) uploaded successfully before you attempt to use any of the uploaded file information. Otherwise, you will end up with blank screens or error messages that don't match the actual problem or follow-on error messages that don't have anything to do with the problem. Quote Link to comment https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308314 Share on other sites More sharing options...
QuickOldCar Posted January 16, 2012 Share Posted January 16, 2012 I totally agree PFMaBiSmAd, my multi-upload form does that, but would be too much for someone to adapt to their use. and as an example of how i do my error checking while(list($key,$value) = @each($_FILES["file"]["name"])) { if(!empty($value)){ if ($_FILES["file"]["error"][$key] > 0) { echo "Error: " . $_FILES["file"]["error"][$key] . "<br/>" ; } else { //continue with upload } Quote Link to comment https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308317 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.