I have problem with downloading files via PHP.
The funny thing is that I can not trace the problem. The code works good for some websites and not good with other. It is loop in PHP that downloads the backup files from websites (there is delay with sleep before requests).
Why I can not trace the problem?
Because when I run manually the code, it works (downloads the file). And when it is run by CRON, sometimes it downloads the file, sometimes it does NOT download the file (only downloads 2 empty new lines).
The download is with curl (I have also tried with different code with fsockopen and fread).
Does anyone have an idea on how I can solve this?
Headers are removed with CURL by setting the correct option.
function fetch_url($url) {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_TIMEOUT, 20);
if ($cookiejar != '') {
curl_setopt($c, CURLOPT_COOKIEJAR, $cookiejar);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookiejar);
}
curl_setopt($c, CURLOPT_HEADER , false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST , false);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER , false);
curl_setopt($c, CURLOPT_FOLLOWLOCATION , true);
curl_setopt($c, CURLOPT_AUTOREFERER , true);
curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12');
$con = curl_exec($c);
curl_close($c);
return $con;
}
echo fetch_url('http://www.example.com/zip.zip');
Try using http://www.php.net/manual/en/function.curl-getinfo.php to display information about the curl request
Also, Maybe it’s in your code elsewhere, but I’m not seeing any content type headers for your echoing of the file