Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308289
Share on other sites

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 :P

 

 

Link to comment
https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308290
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308299
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308300
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308302
Share on other sites

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,

Link to comment
https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308305
Share on other sites

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,

Link to comment
https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308306
Share on other sites

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.";
}

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308307
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308314
Share on other sites

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
}

Link to comment
https://forums.phpfreaks.com/topic/255158-help-with-mime-zip/#findComment-1308317
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.