I've inherited a PHP script which is simply trying to download a .csv file. However, it is not running as expected on the server that hosts the script. On my PC the same script works fine:
Extract of the code below:
$file = 'myfile.csv';
$url = 'http://example.com/documents/myfile.csv';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(curl_exec($ch)!==FALSE){
$urlValid = true;
$info = curl_getinfo($ch);
print_r($info);
$csv = file_get_contents($url);
print_r($csv);
$f = fopen('php://temp', 'r+');
fwrite($f, $csv);
rewind($f);
}
else{
$urlValid = false;
echo 'Url not valid'."
";
exit;
}
php.ini is set appropriately with allow_url_fopen = On
I've added some debug in - $info is returning the correct url, content_type and content_length but $csv is a generic html error page.
I can use curl -O http://example.com/documents/myfile.csv
from the server and the file is downloaded as expected.
As mentioned I can run the script from my PC and the file is downloaded as expected.
I'm out of ideas :(
Versions that may help CentOS 5, PHP 5.1.6