www.webdeveloper.com
Results 1 to 8 of 8

Thread: How to check if uploaded files are mp3?

  1. #1
    Join Date
    Mar 2005
    Posts
    140

    How to check if uploaded files are mp3?

    Searched and searched on the subject, read the mp3 format specifications but found nothing.

  2. #2
    Join Date
    Aug 2006
    Location
    Michigan
    Posts
    1,046
    validate the extension .mp3

    preg_match

  3. #3
    Join Date
    Mar 2005
    Posts
    140
    I am afraid that might not be enough. I need to insure the file is a streamable mp3 file. Otherwise someone might just rename a jpg into mp3 and do the upload.

  4. #4
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    Use mime_content_type to test the actual mimetype as determined by the mimetype field written in the first few bytes of the file. Of course these can be spoofed, but it's more secure then just using regular expressions.
    PHP Code:
    $uploaded 'some_file.mp3';
    //from http://www.webmaster-toolkit.com/mime-types.shtml
    $mp3_mimes = array('audio/mpeg3''audio/x-mpeg-3'); 

    if (!
    in_array(mime_content_type($uploaded), $mp3_mimes)) {
      echo 
    "not a valid mp3";
    } else {
      
    do_whatever();

    Last edited by TJ111; 06-03-2008 at 09:10 AM.

  5. #5
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    At least 98% of internet users' DNA is identical to that of chimpanzees

  6. #6
    Join Date
    Apr 2007
    Posts
    1,664
    When the file is uploaded you have the $_FILES array containing the file information. print_r($_FILES) and you 'll see what it contains.

    One of the array keys is 'type'. That's the mime type of the file. So where the name can be changed the type is read from the file data.

    $_FILES['uploadedfile']['type']

    Rename the file with mp3 extension to be sure it's both the type, and has the mp3 extension you want.

    I would validate by creating an array of mp3 mime types

    I think this is current but do some research to check

    audio/mpeg
    audio/x-mpeg
    audio/mp3
    audio/x-mp3
    audio/mpeg3
    audio/x-mpeg3
    audio/mpg
    audio/x-mpg
    audio/x-mpegaudio

    (list taken from http://filext.com/file-extension/MP3)

    Then compare the files type to see it is in the array (using inarray() )

    If it is continue moving it to the server if not exit with a message to the user.

    If you get complaint from users in the future (after you have it working for a while) that they can't upload first thing to check is no new mime types have been created. This happened recently with Microsoft's new XML office doc types and the acceptable mime types array had to be updated for acceptable office docs.

  7. #7
    Join Date
    Mar 2005
    Posts
    140
    For some unknown reason mime content type doesn't work. mime debug IS switched on in php.ini


    Quote Originally Posted by TJ111
    Use mime_content_type to test the actual mimetype as determined by the mimetype field written in the first few bytes of the file. Of course these can be spoofed, but it's more secure then just using regular expressions.
    PHP Code:
    $uploaded 'some_file.mp3';
    //from http://www.webmaster-toolkit.com/mime-types.shtml
    $mp3_mimes = array('audio/mpeg3''audio/x-mpeg-3'); 

    if (!
    in_array(mime_content_type($uploaded), $mp3_mimes)) {
      echo 
    "not a valid mp3";
    } else {
      
    do_whatever();


  8. #8
    Join Date
    May 2007
    Location
    Cleveland, OH
    Posts
    1,403
    Read this comment (and the one below it) from the php website and see if they help you at all. http://us3.php.net/manual/en/functio...type.php#79166

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles