...

View Full Version : Cannot Get My PHP Script To Upload MP3 Files



dharvell
12-13-2008, 12:50 AM
As the title states, I need to upload MP3 files via PHP. For whatever reason, it just doesn't seem to work. Following is my script:


<?
$need_auth = true;

include("inc/connect.php");
include("inc/session.php");

if(($_FILES["file"]["type"] == "audio/mpeg") && ($_FILES["file"]["size"] < 10485760))
{
if($_FILES["file"]["error"] > 0)
{
echo "Error: " .$_FILES["file"]["error"]. "<br />";
}
else
{
if(file_exists("media/" .$_SESSION["username"]. "/" .$_FILES["file"]["name"]))
{
echo "File already exists.";
}
else
{
$addFile = $_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"],"media/" .$_SESSION["username"]. "/" .$addFile);
echo "Upload successful. Click <a href=\"controlPanel.php?userId=" .$_SESSION["username"]. "\">
<META HTTP-EQUIV=\"refresh\" content=\"0; URL=controlPanel.php?userId=" .$_SESSION["username"]. "\">";
}
}
}
else
{
echo "something went wrong";
}
?>

For the record, this is the HTML form that calls the above script:


<form name="uploadMusic" action="doUploadMusic.php" method="post" enctype="multipart/form-data">
<table width="500" height="500" border="0" cellpadding="0" cellspacing="5">
<tr>
<td width="500" height="25" align="left" valign="top" colspan="2">music upload:</td>
</tr>
<tr>
<td width="200" height="15" align="left" valign="top" class="darkText"><font color="red" size="-2">*</font> track to upload (10MB max):</td>
<td width="300" height="15" align="left" valign="top"><input type="file" name="file"></td>
</tr>
<tr>
<td width="200" height="15"></td>
<td width="300" height="15" align="left" valign="top"><input type="submit" name="submit" value="upload"></td>
</tr>
<tr>
<td width="500" align="left" valign="top" colspan="2"><font color="red" size="-2">by uploading music, you agree that you have read and agree to the <span class="redText" onMouseOver="style=cursor: hand; text-decoration: underline;" onClick="window.open('musicSubmissionAgreement.php','music terms',config='height=480,width=640,toolbar=no,menubar=0,status=0,scrollbars=1');">Music Submission Terms and Agreement</span>.</font></td>
</tr>
</table>
</form>

For kicks and grins, I did a print_r of what was happening. The output was:


Array
(
[name] => Pigs To The Slaughter.mp3
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)

The thing that confuses me is that the image upload script that I modeled this after works just fine. Any ideas?

Thanks.

+dharvell

CFMaBiSmAd
12-13-2008, 12:55 AM
http://us.php.net/manual/en/features.file-upload.errors.php

Your code also needs to check the ['error'] element before it tests anything else about the uploaded file.

dharvell
12-13-2008, 01:00 AM
http://us.php.net/manual/en/features.file-upload.errors.php

Your code also needs to check the ['error'] element before it tests anything else about the uploaded file.

First, thanks for the reply. Thanks, also for pointing out the error meanings. I downloaded the manual, but couldn't find an explanation of the errors, try as I might.

I should have also mentioned that I set my POST_SIZE_MAX to 12MB. I didn't even see the setting for UPLOAD_MAX_FILESIZE. Great to know! I'll give it a go and report back on an edit. Thanks, again!

EDIT:

Yeah... you rock! UPLOAD_MAX_FILESIZE was set to 2MB while the file I was attempting to upload was 6.5MB. Good call. It works great!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum