checking mp3 file

Discussion in 'PHP' started by y2k4ever, May 31, 2008.

  1. #1
    I wanted to write php script so that it can upload mp3 file.
    but is there a way that i can check that it really is an mp3 but not some text file being renamed to .mp3 extension?

    I've looked through some example and found this
    
        function _checkSongFormat($sSongPath)
        {
    		$sSongPath = str_replace(" ","%20",$sSongPath);
            $aMp3Headers = array
            (
                array(0x49, 0x44, 0x33, 0x03),
                array(0xFF, 0xFB),
            );
            $aSongData = array();
            $hF = fopen($sSongPath, 'r');
            for ($i = 0; $i < 4; $i++)
            {
                $aSongData[$i] = bin2hex(fread($hF, 1));
            }
            fclose($hF);
            foreach ($aMp3Headers as $aHeader)
            {
                $bEqual = true;
                for ($i = 0; $i < 4; $i++)
                {
                    if (!isset($aHeader[$i]))
                    {
                        break;
                    }
                    if (dechex($aHeader[$i]) != $aSongData[$i])
                    {
                        $bEqual = false;
                        break;
                    }
                }
                if ($bEqual)
                {
                    return true;
                }
            }
            return false;
        }
    
    Code (markup):
    But here's the problem...even a real mp3 file sometimes return as false.
     
    y2k4ever, May 31, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
  3. slaith

    slaith Active Member

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #3
    i dun get it ...wat do u mean by check an mp3 file?
     
    slaith, Jun 4, 2008 IP
  4. SoftCloud

    SoftCloud Well-Known Member

    Messages:
    1,060
    Likes Received:
    28
    Best Answers:
    2
    Trophy Points:
    120
    #4
    I usually just use this on my audio site:

    if ($_FILES['audio']['type'] == "audio/mpeg") {
    // Continue
    } else {
    // Error
    }
    Code (markup):
    With the input box name as audio. :)
     
    SoftCloud, Jun 4, 2008 IP