Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    66
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Cannot Get My PHP Script To Upload MP3 Files

    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:

    Code:
    <?
      $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:

    Code:
    <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:

    Code:
    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

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,037
    Thanks
    2
    Thanked 316 Times in 308 Posts
    http://us.php.net/manual/en/features...oad.errors.php

    Your code also needs to check the ['error'] element before it tests anything else about the uploaded file.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    dharvell (12-13-2008)

  • #3
    New Coder
    Join Date
    May 2008
    Location
    Michigan
    Posts
    66
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    http://us.php.net/manual/en/features...oad.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!
    Last edited by dharvell; 12-13-2008 at 01:14 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •