Vigilant Psyche Posted July 4, 2008 Share Posted July 4, 2008 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 More sharing options...
br0ken Posted July 4, 2008 Share Posted July 4, 2008 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 https://forums.phpfreaks.com/topic/113241-restricting-mime-file-types-on-upload/#findComment-581775 Share on other sites More sharing options...
Vigilant Psyche Posted July 4, 2008 Author Share Posted July 4, 2008 Okay, thanks. After doing what you suggested I decided to revert to using file extensions. Now, does anyone know how to split the file extension, (after the dot), of from $_FILES["file"]["name"]? Link to comment https://forums.phpfreaks.com/topic/113241-restricting-mime-file-types-on-upload/#findComment-581814 Share on other sites More sharing options...
corbin Posted July 4, 2008 Share Posted July 4, 2008 Easiest way: $file = 'some.file.txt'; $tmp = explode('.', $file); $ext = $tmp[count($tmp)-1]; Link to comment https://forums.phpfreaks.com/topic/113241-restricting-mime-file-types-on-upload/#findComment-581817 Share on other sites More sharing options...
Vigilant Psyche Posted July 4, 2008 Author Share Posted July 4, 2008 Yay it works! Thanks, guys! Link to comment https://forums.phpfreaks.com/topic/113241-restricting-mime-file-types-on-upload/#findComment-581837 Share on other sites More sharing options...
br0ken Posted July 4, 2008 Share Posted July 4, 2008 <?php $var = "a.file.name.jpg"; echo substr($var, strrpos($var, ".")); ?> Link to comment https://forums.phpfreaks.com/topic/113241-restricting-mime-file-types-on-upload/#findComment-581903 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.