Code
Using PHP built in function fopen we can read a remote stream, read the contents in the buffer and flush it to the file.
- // Copy remote file locally
- $remote_file='http://www.example.com/music.mp3';
- // open file
- if($fp_remote=fopen($remote_file,'rb')){
- // local filename
- $local_file='/tmp/music.mp3';
- // read buffer
- if($fp_local=fopen($local_file,'wb')){
- while($buffer=fread($fp_remote,8192)){
- // write buffer in file
- fwrite($fp_local,$buffer);
- }
- // close local
- fclose($fp_local);
- }
- // close remote
- fclose($fp_remote);
- }