3

SO i have a url of an mp3 file and i need to save it to a specific directory. How is the best way to do this, someone told me curl but is there a copy file of some php command that will make this process easier

4 Answers 4

16
copy($url, $destination);

copy

Sign up to request clarification or add additional context in comments.

2 Comments

great answer! one additional hint: user 'dirname(...)' or an equivalent for $destination to get the relative server path
NOTE: The destination must include the file name and the directory must exist.
1
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://amanjain.com/path-to-mp3.mp3');
//Create a new file where you want to save
$fp = fopen('filename.mp3', 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec ($ch);
curl_close ($ch);
fclose($fp);
?>

Comments

1

Very easy :-D

$URL = ...; //  Like "http:// ...."
$FileToSave = ...;     //  Like "/home/.." or "C:/..."
$Content = file_get_contents($URL);
file_put_contents($FileToSave, $Content);

Hope this helps.

Comments

0

Make sure also you also modify your pages allowed execution time. Most pages only allow 30 seconds, especially if you are doing this over a HTTP page (a webpage). You need to up this to match how long it actually takes to download the file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.