Upload file from URL to Server using cURL in PHP
This Post show how to upload data or audio files on server getting from URL and how to download that file in PC using URL.
First put URL in string like below:
$url = 'http://s.cdnpk.eu/pk-mp3/love-dose/s165352040.mp3';
To Download file in PC its a simple short-cut.using download attribute of <a> tag in HTML.
echo "<a href='".$url."' download='myfile.wav'>Download</a>";
Now convert that URL to file using curl.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
Now create a file on server. And open it in write mode.
$fileName = "myfile.wav";
$fh = fopen($fileName, 'w') or die("can't open file");
Write the file which we fetch from URL in 'myfile'. it was in $data variable.
fwrite($fh, $data);
fclose($fh);
Close the file that's it. Happy coding. :)
Post a Comment