Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 1 of 1
  1. #1
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Hey folks. When you come out of this tutorial you should be able to write your own mp3 uploader using PHP and X/HTML.

    What you need to know

    - basic html
    - basic php syntax
    - In php the default value for the MAX_FILE_SIZE is 2 mb. Your host may have changed this.

    What you need to have

    - php enabled host

    Lets get coding.

    First, lets design the form that will take the user's selected file.

    Code:
    <body>
    
    <p style="font-family: verdana;">Please select a file to upload: </p>
    
    <br />
    
    <form action="<?php echo $PHP_SELF ?>" method="post" enctype="multipart/form-data">
    
    <input type="file" name="file" size="40" />
    
    <br /><br />
    
    <input type="submit" name="submit" value="Upload" />
    
    <br /><br />
    
    <span style="font-family: verdana; font-size: 12px;">* only mp3 files under 2 megabytes are allowed.</span>
    
    </form>
    
    </body>
    Notice the <form> tag. If your a newbie to forms, the enctype="multipart/form-data" may well be new to you. It is needed when uploading files.

    Moving on, we now know that our user bob wants to upload a file, but, it would be bad for us to just take his word for it and believe its a .mp3.

    So lets check that out.

    PHP Code:
    <?php
    if($_POST['submit']=="Upload") { // if user clicked upload
      
    if ($_FILES['file']['name'] != "") { // if the file input is not empty
       
    if (($_FILES['file']['type'] == "audio/mpeg") || ($_FILES['file']['type'] == "application/force-download")) { // if the file is a .mp3 file
                    
    if ($_FILES["file"]["size"] < 2097152) { // if the file is under 2 megabytes
    ?>
    Wow. thats a lot of code.

    The first if statement checks to see if the form has been submitted. We need this so that when the page is accessed, the script does not try and upload a file.

    The second if statement is to make sure that the user entered a filename into the input field. We need this because we need to know what file bob wants to upload.

    The third if statement checks to see if the file bob wants to upload is a .mp3 file

    I had trouble with this as I was testing it out in Firefox and Firefox's MIME type for an mp3 file is "application/force-download" and in IE it is "audio/mpeg" so that is why there is a OR in the code.

    The final if statement checks that the size of the given file is small enough to be uploaded.

    Lets say bob chose a file which is called balloon.mp3 and with a size of 1.5 mb, we would want to upload it. Heres how

    PHP Code:
                            move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" $_FILES["file"]["name"]);
    echo 
    "File has been stored in your uploads directory."
    Very well. That is assuming all of our conditions are met.

    Now we have to reverse the order and write code to deal with the chance of one (or indeed more than one) not being met.

    PHP Code:
    }
    else {    echo 
    "Please upload a file that is under 2 mb!";}
    } else {
        echo 
    "Please upload a mp3 file!";
        exit;}
    } else {     echo 
    "Please enter a file.";}
    }
    ?> 
    To save you copying and pasting like a maniac, here is the file in all it's glory

    PHP Code:
    <html>

    <head>

    <title>Upload MP3</title>

    <?php

    if($_POST['submit']=="Upload") {
        if (
    $_FILES['file']['name'] != "") {
                if ((
    $_FILES['file']['type'] == "audio/mpeg") || ($_FILES['file']['type'] == "application/force-download")) {
                    if (
    $_FILES["file"]["size"] < 2097152) {            
                            
    move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" $_FILES["file"]["name"]);
                           echo 
    "File has been stored in your uploads directory.";}
    else {    echo 
    "Please upload a file that is under 2 mb!";}
    } else {
        echo 
    "Please upload a mp3 file!";
        exit;}
    } else {     echo 
    "Please enter a file.";}
    }
    ?>

    </head>

    <body>

    <p style="font-family: verdana;">Please select a file to upload: </p>

    <br />

    <form action="<?php echo $PHP_SELF ?>" method="post" enctype="multipart/form-data">

    <input type="file" name="file" size="40" />

    <br /><br />

    <input type="submit" name="submit" value="Upload" />

    <br /><br />

    <span style="font-family: verdana; font-size: 12px;">* only mp3 files under 2 megabytes are allowed.</span>

    </form>

    </body>

    </html>
    Hope you find it useful.

  2.  


Remove Ads

Posting Permissions

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

Search tags for this page

free script to upload mp3s

,

how to upload mp3 song in php

,

mp3 file upload code in php

,
mp3 upload script download
,
php audio upload script
,
php code to upload mp3
,

php mp3 upload script

,

php upload mp3

,
php upload mp3 example
,
upload mp3 files html code
Click on a term to search for related topics.
All times are GMT -6. The time now is 10:10 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com