Jump to content

Restricting MIME file types on upload.


Recommended Posts

Hi, guys. I'm trying to resrict file types with this script but when a user tries to upload a rar compressed folder, which is specified first in the array, it gives the error:

 

The file the you have attempted to upload isn't in our list of allowed file types above.

 

So, have I mixed up ther MIME file types or something? Thanks for any help. :)

 

 

 

 

						<form action="index.php?body=upload" method="post"
					enctype="multipart/form-data">
					<label for="file">Filename:</label>
					<input type="file" name="file" id="file" />
					<input type="submit" name="submit" value="Upload" />
					</form>

					<?php

					if ($_FILES["file"]["size"]!=0)
					{
						$types="application/x-rar-compressed,image/jpg,image/jpeg,image/pjpeg,image/gif,image/png,application/ogg,application/zip,audio/mpeg,audio/x-wav,text/plain,text/xml,application/x-shockwave-flash,application/pdf,audio/sp-midi";
						$types=explode(",", $types);

						$good=0;

						foreach($types as $ext)
						{
							if($_FILES["file"]["type"] == $ext)
							{
								$good=1;

							}
						}

						if ($good==0)
						{
							echo "The file the you have attempted to upload isn't in our list of allowed file types above.";
						}
						else
						{
							if($_FILES["file"]["size"] < 2000000000000000000000000000000000000)
							{
								 move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/".$_COOKIE['user']."/".$_FILES["file"]["name"]);
								 echo "File uploaded successfully.";
							}
							else
							echo("The file is over the size limit.<br>File size limit: 20,000 bytes. Your file size: ".$_FILES["file"]["size"]." bytes.<br>Try compressing the file further.");
						}
					}

					?>

Link to comment
https://forums.phpfreaks.com/topic/113241-restricting-mime-file-types-on-upload/
Share on other sites

Try echoing the value in $ext and the file type when iterating through your loop. This way you can see exactly what two values are being compared and whether it's working ok.

 

Try replacing you loop with this...

 

<?php

foreach($types as $ext)
{
echo "ext: $ext<br />";
echo "Type: ".$_FILES["file"]["type"]."<br /><br />";

if($_FILES["file"]["type"] == $ext)
{
	$good=1;
}
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.