Jump to content

Upload script not returning mime type


Krash

Recommended Posts

 

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: &nbsp<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?

 

 

Link to comment
Share on other sites

 

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()?

 

 

 

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

$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.

 

Link to comment
Share on other sites

 

Ok, took a guess and added the error check to the code -

 

{
echo "File type: &nbsp<b><font face='arial'>" . $uploaded_type . "</font></b><br><br>";
echo "File error: &nbsp<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.

 

:shrug:

 

 

Link to comment
Share on other sites

 

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"!

 

 

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.