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

?>

Link to comment
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.