How to download an mp3 file from remote url?
Use CURL to download, then you can use file_get_contents to save file on server, or you can use some headers to force download the file.
$ch = curl_init('http://url-to-file.com/audio.mp3');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200) {
file_put_contents(dirname(__FILE__) . '/audio.mp3', $output);
}
Download to browser:
$ch = curl_init('http://url-to-file.com/audio.mp3');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200) {
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=audio.mp3");
echo $output;
die();
}
Downloading Mp3 file from remote. Node js
The problem here is that http
doesn't follow redirects.
You can use the request npm module that does it by default to avoid handling headers yourself.
var fs = require('fs'),
request = require('request');
request
.get('http://foo.com/bar.mp3')
.on('error', function(err) {
// handle error
})
.pipe(fs.createWriteStream('2.mp3'));
How to download a part of mp3 from server?
Its is not possible the way you want to do it. mp3 files do not have timestamps. If you just jump to the middle of an mp3, (and look for the frame start marker), then start decoding, You have no idea at what time this frame is for, because frames are variable size. The only way to know, is to count the number of frames before the current position. Which means you need the whole file.
php script to download files from a remote URL to the user's computer directly
You could achieve this by using the following PHP:
$file_name = $_GET['filename'];
$file_url = 'http://path_to_audio_folder.com/' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
exit;
You could then call it from your script like: download.php?filename=my_song.mp3
and it would be forced as a download to your visitor clicking on it.
getid3: download the full mp3 data from remote server
ID3v1 tags are at the end of the file, not at the beginning. Therefore, you won't be able to read them until you either download a chunk of the end of the file (with range requests done with cURL), or download the entire file.
If you want ID3v2, those will be at the beginning of the file.
Related Topics
Laravel PHP Fatal Error: Require(): Failed Opening Required ... Bootstrap/Autoload.Php on Line 17
How to Remove Back Slashes from Json Output in PHP
How to Send Data Using Redirect With Laravel
How to Store Values from Foreach Loop into an Array
Phpexcel Write Long Number in Cell
How to Unlink (Delete) an Image in Codeigniter
How to Properly Encode Utf-8 for JavaScript and Json
How to Do Multilevel Menu the Correct Way With MySQL PHP
Change the Maximum Upload File Size
Showing All Errors and Warnings
Mysql Return Result If 0 Rows Returned
How to Show Checkboxes as Checked When Values Are Set in the Database in Laravel
How to See If a User Is Online in a Website With PHP and MySQL Driven Databases
How to Pass an Array Within a Query String
Convert Image to Base 64 String With Laravel
How to Install and Use PHP on Macos Via Brew