Krash Posted July 6, 2010 Share Posted July 6, 2010 I'm running an upload script that allows users to upload mp3 files only. One of my members has an old Mac, is severely technophobic, but has used the uploader successfully with an old version of Camino. Now he can't get it to work with the same setup. If a user tries to upload a file that's not an mp3, the script returns an error message that includes the invalid mime type. When my member tries to u/l an mp3, he gets the error message, but the file type is blank. I am unable to duplicate the problem in Win IE or FF. Here's the code - <?php $target = "music/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; if ($uploaded_size > 10000000) { echo "Your file is too large - 10MB max.<br><br>"; $ok=0; } if ($uploaded_name=="") { echo "Please select a file to upload.<br><br>"; $ok=0; $duh=1; } else if (($uploaded_type != "audio/mpeg") && ($uploaded_type != "audio/x-mpeg") && ($uploaded_type != "audio/mp3") && ($uploaded_type != "audio/x-mp3") && ($uploaded_type != "audio/mpeg3") && ($uploaded_type != "audio/x-mpeg3") && ($uploaded_type != "audio/mpg") && ($uploaded_type != "audio/x-mpg") && ($uploaded_type != "audio/x-mpegaudio") && ($uploaded_type != "audio/x-amzaudio")) { echo "File type:  <b><font face='arial'>" . $uploaded_type . "</font></b><br><br>"; echo "You may only upload mp3 files.<br><br><br>"; $ok=0; } if (($ok==0) && ($duh!=1)) { echo "Sorry, your file was not uploaded."; } else { if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "Your file has been uploaded to - <br><br>"; echo "<font face='arial' size='3'>http://www.xxxxxxxxxxxx.com/xxxxx/music/" . $uploaded_name . "</font><br><br>"; } else { if ($duh!=1) { echo "Sorry, there was a problem uploading your file.<br><br>"; } } } ?> Why is it not returning the mime type? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2010 Share Posted July 6, 2010 Are you testing if the $_FILES array is empty or not or the $_FILES['uploaded']['error'] element in your code to make sure something else is not causing the problem? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 6, 2010 Share Posted July 6, 2010 The browser provides the mime type and it is not reliable. You can check the extension (not reliable) or use some magic: mime_content_type() or fileinfo_file(). Quote Link to comment Share on other sites More sharing options...
Krash Posted July 6, 2010 Author Share Posted July 6, 2010 If $_FILES is empty, then $uploaded_name is also empty, and prompts the user to select a file. The problem appears to be that $uploaded_type is null - it doesn't match any of the allowed mime types, and is returned as null in the error message. The script is seeing a file name, but is generating a blank file type. I've tried butchering a file name many different ways, but the script always returns a file type, even if it's incorrect. Ok, just saw the next post re: browser providing file type. This same user had originally tried Firefox, but his old version gave an invalid file type (it's a documented bug). When he switched tp Camino, it worked, but now it doesn't. How do I use mime_content_type() or fileinfo_file()? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 6, 2010 Share Posted July 6, 2010 If $_FILES is empty, then $uploaded_name is also empty, and prompts the user to select a file. The problem appears to be that $uploaded_type is null - it doesn't match any of the allowed mime types, and is returned as null in the error message. The script is seeing a file name, but is generating a blank file type. I've tried butchering a file name many different ways, but the script always returns a file type, even if it's incorrect. Ok, just saw the next post re: browser providing file type. This same user had originally tried Firefox, but his old version gave an invalid file type (it's a documented bug). When he switched tp Camino, it worked, but now it doesn't. How do I use mime_content_type() or fileinfo_file()? Which one you use depends on your PHP version. You'll need to check the tmp_name instead. Also, I just looked at your code and I don't see where $uploaded_type gets a value. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2010 Share Posted July 6, 2010 There's about a half-dozen different upload errors that will result in a file name, but no mime type because the upload failed. Since you did not bother to mention that you were testing the ['error'] element, I'll guess you are not. Quote Link to comment Share on other sites More sharing options...
Krash Posted July 6, 2010 Author Share Posted July 6, 2010 $uploaded_type and $uploaded_size both apparently get their values from the form submit. Don't know enough about php to make a better guess, but if they have correct values when I use the form and script, should work for everyone, since it's server-side. Believe I'm using php 5, but I'll have to check. It's either 5 or 4. There's about a half-dozen different upload errors that will result in a file name, but no mime type because the upload failed. Since you did not bother to mention that you were testing the ['error'] element, I'll guess you are not. Don't know how to do that. Quote Link to comment Share on other sites More sharing options...
Krash Posted July 7, 2010 Author Share Posted July 7, 2010 Ok, took a guess and added the error check to the code - { echo "File type:  <b><font face='arial'>" . $uploaded_type . "</font></b><br><br>"; echo "File error:  <b><font face='arial'>" . $_FILES['uploaded']['error'] . "</font></b><br><br>"; echo "You may only upload mp3 files.<br><br><br>"; $ok=0; } It's returning "0" when I try to u/l a file that's not mp3, even though the upload fails. Quote Link to comment Share on other sites More sharing options...
Krash Posted July 7, 2010 Author Share Posted July 7, 2010 OK, fixed it last night. There were two needles in this haystack that I needed - The browser provides the mime type Which one you use depends on your PHP version. The uploader was originally working because I was running php4 on the server. I changed it to php5 while working on the next project, and forgot about it. Next time my member used the uploader, it wouldn't work. Apparently, his old version of Camino is submitting a deprecated mime type value - php4 recognizes it, but php5 does not. He uploaded a file this a.m. successfully. Thanks for the help - you can mark this thread "SOLVED"! Quote Link to comment 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.