File download PHP script doesn't work because of server delay - php

I have this code to download a file, but on sourceforge.net sever there is a 5 seconds delay before file starts to download (You can see it if you try to load this link in browser). And I have file with zero size after script is done. How can I download this file? Thanx in advance!
$url = 'http://downloads.sourceforge.net/project/gnucash/gnucash%20%28stable%29/2.4.9/gnucash-2.4.9-setup.exe';
$ch = curl_init($url);
$fp = fopen('/home/content/11/8564211/html/'.substr($url,strrpos($url,'/'),strlen($url)), 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Sourceforge uses a meta refresh tag to start the download and because CURLOPT_FOLLOWLOCATION responds to Location: header it will most likely not help.
I think you're going to have to do som HTML parsing to achieve what you want to do. You have to find this line:
<meta http-equiv="refresh" content="5; url=http://downloads.sourceforge.net/project/gnucash/gnucash%20%28stable%29/2.4.9/gnucash-2.4.9-setup.exe?r=&ts=1333621946&use_mirror=switch">
Then you must get the url from the line and load that.
It's possible that Sourceforge uses some cookie or session based stopper for this kind of downloads so you may have to compensate for that.
I haven't tested this but it looks like this is close to the way you have to do this.

You can try this:
$url = 'http://downloads.sourceforge.net/project/gnucash/gnucash%20%28stable%29/2.4.9/gnucash-2.4.9-setup.exe';
$ch = curl_init($url);
$fp = fopen('/home/content/11/8564211/html/'.substr($url,strrpos($url,'/'),strlen($url)), 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);
fclose($fp);

To avoid timeouts in PHP you can use:
set_time_limit($hugetimeout);
before your script. You can read further documentation here.

On the download page, there's a direct link, you could try using that instead ?

Related

Download a file from Dropbox with cURL

i would like to get some help about the following problem. I'm under windows and i'm able to download any files using the cURL, but when it comes to download from Dropbox i'm unable to do it. Even if i use ?raw=1 or ?dl=1 which is responsible to redirect me to the file i still can't do it.
Here is the script i'm using:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'any url?raw=1');
$fp = fopen('backup.wpress', 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec ($ch);
curl_close ($ch);
fclose($fp);
Thanks in advance. I would be very grateful for any suggestions and help.
There's a 302 redirect on that URL, so you'll need to add the line
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
(You also might want to edit that URL out the post, not sure if it's sensitive data...)

Download image using PHP but image is htaccess redirected?

I want to download an image to my server using PHP. This image's html only allows target="_self" meaning it can only be downloaded from the browser apparently. I try to access the image directly in the browser and I get redirected. Is there any way to download this image onto my server via PHP? Maybe I'm missing an option in cURL?
Thanks!
Yes, you have to tell CURL to follow redirects --- try this function:
function wgetImg($img, $pathToSaveTo) {
$ch = curl_init($img);
$fp = fopen($pathToSaveTo, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}

cURL script will not download images; Instead renders junk

I have been using the following function to download pictures from a distributor for use on our website as was described here:
$url = "http://covers.stl-distribution.com/7819/lg-9781936417445.jpg";
$itemnum = 80848;
$path = "www.gullions.com/localstore/test/test.jpg";
header('Content-type: image/jpeg');
$ch = curl_init($url);
$fp = fopen("http://www.gullions.com/localstore/test/test.jpg", 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Instead of downloading and saving the picture, it only prints crazy characters to the screen. I know that means that for some reason the browser is trying to render the picture but most likely doesn't recognize the file type. But I can't seem to find the problem. You can see the crazy output by navigating here. To verify that the image wasn't downloaded and saved, you can navigate here. Also, FTP also shows that no file was downloaded. If you navigate to the original picture's download url you'll see that the file we are trying to download does in fact exist.
I have contacted my host and verified that no settings have been changed with the server, that cURL is functioning properly, and have even rolled back my browser to verify that a recent update didn't cause the issue. I created a test file by removing the function from the application and have tried running it separately but have only had the same results.
Add:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
as by default this parameter is false. Docs read:
TRUE to return the transfer as a string of the return value of
curl_exec() instead of outputting it out directly.
EDIT
Setting CURLOPT_RETURNTRANSFER make curl_exec() return data, so it should be written manually, like this:
$url = "http://covers.stl-distribution.com/7819/lg-9781936417445.jpg";
$ch = curl_init($url);
$fp = fopen("./test.jpg", 'wb');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HEADER, 0);
fwrite($fp, curl_exec($ch));
curl_close($ch);
fclose($fp);
Also this code, that uses CURLOPT_FILE works for me just fine:
$url = "http://covers.stl-distribution.com/7819/lg-9781936417445.jpg";
$ch = curl_init($url);
$fp = fopen("./test.jpg", 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
so I basically suspect that your file handle is not valid, therefore cURL falls back to default output. Try this code with elementary error checking (you should ALWAYS check for errors):
$url = "http://covers.stl-distribution.com/7819/lg-9781936417445.jpg";
$fp = fopen("./test.jpg", 'wb');
if( $fp != null ) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
} else {
exit('ERROR: Failed to open file');
}
Note that my examples write to the same folder scripts sits in. It must work unless your server got file permissions messed badly. If it works for you, then investigate if (usually) user your scripts runs as can write to your target folder.
You haven't told your browser what type of file to expect, so it's probably defaulting to text/plain.
You need at least:
header('Content-type: image/jpeg');
As well, curl by default outputs whatever it fetches, unless you explicitly tell it you want to have the fetched data returned, or saved directly to file.

How to ensure download does not breaks using PHP Curl

I have using PHP's Curl function to download a list of pdfs from the backend. But sometimes some pdfs are corrupted.
I think, this happens because the downloads breaks and it will start to download the next pdf before completing the previous download.
Any idea how to prevent this? I am using the code below:
function downloadAndSave($urlS,$pathS)
{
$fp = fopen($pathS, 'w');
$ch = curl_init();
curl_setopt($ch,CURLOPT_PROXY,"http://test:1234");
curl_setopt($ch,CURLOPT_PROXYPORT,1234);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch,CURLOPT_URL,$urlS);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
fclose($fp);
}
I have tried using CURLOPT_CONNECTTIMEOUT but no difference. Any other way to prevent this?

Downloading a file and saving to a directory in PHP

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
copy($url, $destination);
copy
<?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);
?>
Very easy :-D
$URL = ...; // Like "http:// ...."
$FileToSave = ...; // Like "/home/.." or "C:/..."
$Content = file_get_contents($URL);
file_put_contents($FileToSave, $Content);
Hope this helps.
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.

Categories