I try to download file using a php-script from an URL like the following:
http://www.xcontest.org/track.php?t=2avxjsv1.igc
The code I use looks like the following, but it produces empty download files only:
$DLFile= "testfile.igc";
$DLURL="http://www.xcontest.org/track.php?t=2avxjsv1.igc";
$fp = fopen ($DLFile, 'w+');
$ch = curl_init($DLURL);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
fclose($fp);
An other strange thing is when entering the URL in the web browser I don't get the file. It can I could only download the file when clicking the link on the web site!.
Any advice is very appreciated!
To download a file with Curl, use the --output or -o command-line option. This option allows you to save the downloaded file to a local drive under the specified name. If you want the uploaded file to be saved under the same name as in the URL, use the --remote-name or -O command line option.
Consequentially, the file will be saved in the current working directory. If you want the file saved in a different directory, make sure you change current working directory before you invoke curl with the -O, --remote-name flag!
For those of you want to copy the cURL output in the clipboard instead of outputting to a file, you can use pbcopy by using the pipe | after the cURL command. Example: curl https://www.google.com/robots.txt | pbcopy . This will copy all the content from the given URL to your clipboard.
@Chris' answer works, but this seems to work better to download very large files without running out of memory, since it doesn't download the whole file into a variable before writing to disk:
$file_url = 'http://www.test.com/images/avatar.png';
$destination_path = "downloads/avatar.png";
$fp = fopen($destination_path, "w+");
$ch = curl_init($file_url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
$st_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);
if($st_code == 200)
echo 'File downloaded successfully!';
else
echo 'Error downloading file!';
Source: https://www.kodingmadesimple.com/2018/02/php-download-file-from-url-curl.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With