#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    PHP extract function on mp3 file?


    audio file upload code

    Code:
    $allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
    $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    
    if (($_FILES["file"]["size"] < 800000000000000000000000000000000000)
    && in_array($extension, $allowedExts))
    
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    
      if (file_exists("upload/" . $_FILES["file"]["name"]))
    	
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
         move_uploaded_file($_FILES["file"]["tmp_name"],
          "upload/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "upload/" . $_FILES["file"]["name"];*/
    	 	   
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    Extract Code

    Code:
      $filename = $_FILES["file"]["name"];
    $start = 15; //start time marker in seconds
    $end = 600; //end time marker in seconds
     
    $mp3 = new Mp3($filename);
    $extract = $mp3->extract($start,$end);
    if($extract===false)
    {
        die("Error!");
    }
    print "File created : $extract";
    ?>

    the above code works fine.. only if i put the audio file name like this,

    Code:
     $filename = music.mp3;
    otherwise, the extract file is created with 0 bytes...
    Code:
    $filename = $_FILES["file"]["name"];
    Extract Function

    Code:
    function Mp3($file)
        {
            $this->strFile = $file;
            $getID3 = new getID3;
            $this->arrMp3Info = $getID3->analyze($file);
            $this->frameSize = $this->arrMp3Info['mpeg']['audio']['framelength'];
            $this->fileSize           = $this->arrMp3Info['filesize'];
            $this->strFileName        = $this->arrMp3Info['filename'];
            $this->strPlayTimeSeconds = $this->arrMp3Info['playtime_seconds'];
            $this->strPlayTimeString  = $this->arrMp3Info['playtime_string'] ;
            $this->strDataFormat      = $this->arrMp3Info['audio']['dataformat'];
            $this->strChannels        = $this->arrMp3Info['audio']['channels'];
            $this->strBitRate         = $this->arrMp3Info['audio']['bitrate'];
            $this->strChannelMode     = $this->arrMp3Info['audio']['channelmode'];
        }
    
    function extract($start,$end)
        {
            $token = uniqid("my_mp3_");
            $mp3file = "$token.mp3";
            $OutputFile = UPLOAD_DIR."$mp3file";
     
            $startOffSet = $this->arrMp3Info['avdataoffset'];
            $startBytes = $startOffSet+((($start * 1000)/26) * $this->frameSize);
            $endBytes   = $startOffSet+((($end * 1000)/26) * $this->frameSize);
            $InputFile = $this->strFile;
            if($fp_output = @fopen($OutputFile, 'wb'))
            {
                if ($fp_source = @fopen($InputFile, 'rb'))
                {
                    $CurrentOutputPosition = ftell($fp_output);
                    // copy audio data from first file
                    fseek($fp_source, $startBytes, SEEK_SET);
                    while (!feof($fp_source) && (ftell($fp_source) < $endBytes))
                    {
                     fwrite($fp_output, fread($fp_source, 32768));
                    }
                    fclose($fp_source);
                }
                fclose($fp_output);
     
                return $OutputFile;
            }
            return false;
        }
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    An uploaded file's location is stored in 'tmp_name', not 'name'
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by ManiacDan
    An uploaded file's location is stored in 'tmp_name', not 'name'
    this works
    Code:
    $filename ="upload/" . $_FILES["file"]["name"];

    thank you...
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    No, this does not work. Letting users specify the storage path is a no-no.

    Please read the security warning in your other thread.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by Jacques1
    No, this does not work. Letting users specify the storage path is a no-no.

    Please read the security warning in your other thread.
    how to store the extracted file to user specified location? to his local system.... now its storing in the current folder...

    Code:
    $OutputFile = UPLOAD_DIR."$mp3file";
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    You want a user to download the file they just uploaded?
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by ManiacDan
    You want a user to download the file they just uploaded?
    No.No.. Audio cutter...
    the user uploaded the audio file.. it was cut by the extract function based on the user specified start and end timings..

    when user uploads a file, i wan to show 'wait upload in progress' message or progress bar..
    after the download completes the message or progress bar should be closed automatically...

    how to do it?
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by RomanticRaj
    when user uploads a file, i wan to show 'wait upload in progress' message or progress bar..
    after the download completes the message or progress bar should be closed automatically...

    how to do it?
    You already have a thread for that.

    Please don't ask the same question in different places. It will lead to a lot of confusion.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo