Way to get remote file info

Locked
no_more
User
Posts: 1
Joined: Wed Apr 10, 2013 7:03 am
Are you a spambot?: no

Way to get remote file info

Post by no_more » Wed Apr 10, 2013 7:35 am

Is there any way to get remote file info using getID3? I want to get tags from a remote mp3 file.

James Heinrich
getID3() v1 developer
Posts: 1444
Joined: Fri May 04, 2001 4:00 pm
Are you a spambot?: no
Location: Northern Ontario, Canada
Contact:

Re: Way to get remote file info

Post by James Heinrich » Wed Apr 10, 2013 12:50 pm

getID3 operates on local files. Quoting from readme.txt:
To analyze remote files over HTTP or FTP you need to copy the file
locally first before running getID3(). Your code would look something
like this:

Code: Select all

// Copy remote file locally to scan with getID3()
$remotefilename = 'http://www.example.com/filename.mp3';
if ($fp_remote = fopen($remotefilename, 'rb')) {
    $localtempfilename = tempnam('/tmp', 'getID3');
    if ($fp_local = fopen($localtempfilename, 'wb')) {
        while ($buffer = fread($fp_remote, 8192)) {
            fwrite($fp_local, $buffer);
        }
        fclose($fp_local);

		// Initialize getID3 engine
		$getID3 = new getID3;

		$ThisFileInfo = $getID3->analyze($filename);

        // Delete temporary file
        unlink($localtempfilename);
    }
    fclose($fp_remote);
}
That will grab the whole remote file, which can be very slow. You may wish to use this more compact code block which grabs the first 32kB of a file for analysis, which may or may not be sufficient to get all the data that's important to you:

Code: Select all

$filename = tempnam('/tmp','getid3');
if (file_put_contents($filename, file_get_contents('http://getid3.org/demo/test.mp3', false, null, 0, 32768))) {
   if (require_once('/path/to/getid3/getid3.php')) {
      $getID3 = new getID3;
      $ThisFileInfo = $getID3->analyze($filename);
      echo '<pre>'.print_r($ThisFileInfo, true).'</pre>';
   }
   unlink($filename);
}
In any case, it's left up to you to get the remote file data locally for analysis.

Locked