简体   繁体   中英

How to download a wav file from a web location to local server directory using PHP script

I would like to download this .WAV file to my webdirectory

/home/usename/savewavfiles/

I need a PHP script to perform this task or any sample code or tutorial would be a great help.

thanks, sandeep

If you have curl enabled:

<?php
/*
This is usefull when you are downloading big files, as it
will prevent time out of the script :
*/
set_time_limit(0);
ini_set('display_errors',true);//Just in case we get some errors, let us know....

$fp = fopen (dirname(__FILE__) . '/my.way', 'w+');//This is the file where we save the information
$ch = curl_init('http://www.kookoo.in/recordings/sandeepeecs/useraudiofile2277.wav');//Here is the file we are downloading
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

Source

you can do something like this:

$link = 'http://remote.server.com/directory/sound_file.wav';
$my_dir = '/home/usename/savewavfiles/';

// get contents of the remote file
$remote_file = file_get_contents($link);

// create local file
$handle = fopen($mydir . basename($link), 'w');

// fill the local file with the contents from the remote server
fwrite($handle, $remote_file);

// close the local file
fclose($handle);

you can also use linux's wget :

$link = 'http://remote.server.com/directory/sound_file.wav';
shell_exec('wget '. $link);

That's easy for small ( < memory) files:

file_put_contents(
    'localfile.wav',
    file_get_contents('http://example.org/remotefile.wav')
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM