doucai9270 2016-08-15 13:13
浏览 21

php get_file_contents失败

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?