Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用CURL从URL下载文件_Php_Curl_Download - Fatal编程技术网

Php 使用CURL从URL下载文件

Php 使用CURL从URL下载文件,php,curl,download,Php,Curl,Download,我尝试使用php脚本从URL下载文件,如下所示: http://www.xcontest.org/track.php?t=2avxjsv1.igc 我使用的代码如下所示,但它只生成空下载文件: $DLFile= "testfile.igc"; $DLURL="http://www.xcontest.org/track.php?t=2avxjsv1.igc"; $fp = fopen ($DLFile, 'w+'); $ch = curl_init($DLURL); curl_setopt($

我尝试使用php脚本从URL下载文件,如下所示:

http://www.xcontest.org/track.php?t=2avxjsv1.igc
我使用的代码如下所示,但它只生成空下载文件:

$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);
另一件奇怪的事情是,当在web浏览器中输入URL时,我没有得到文件。我只能在点击网站上的链接时下载文件

非常感谢您的建议

试试看

<?php

    $output_filename = "testfile.igc";

    $host = "http://www.xcontest.org/track.php?t=2avxjsv1.igc";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $host);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, false);
    curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $result = curl_exec($ch);
    curl_close($ch);

    print_r($result); // prints the contents of the collected file before writing..


    // the following lines write the contents to a file in the same directory (provided permissions etc)
    $fp = fopen($output_filename, 'w');
    fwrite($fp, $result);
    fclose($fp);
?>

# 或者如果你想把它放在一个循环中,用来解析几个链接。。。你需要一些功能。。这里有一个粗略的想法

<?php

    function collect_file($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, false);
        curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $result = curl_exec($ch);
        curl_close($ch);
        return($result);
    }

    function write_to_file($text,$new_filename){
        $fp = fopen($new_filename, 'w');
        fwrite($fp, $text);
        fclose($fp);
    }


    // start loop here

    $new_file_name = "testfile.igc";
    $url = "http://www.xcontest.org/track.php?t=2avxjsv1.igc";

    $temp_file_contents = collect_file($url);
    write_to_file($temp_file_contents,$new_file_name)

    // end loop here
?>

@Chris的答案是可行的,但下载非常大的文件而不耗尽内存似乎效果更好,因为它在写入磁盘之前不会将整个文件下载到变量中:

$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!';

来源:

在使用Curl之前,我在显示(文件下载对话框)时遇到了一些问题:

// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, $url); 
// Other Curl options ....
$output = curl_exec($ch);
if (isset($_POST["downloadExcelFile"])){ 
        // in my code the "downloadExcelFile" field
        // is sent when i'm trying to download an excel file
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="file.xls"');
        header('Cache-Control: max-age=0');
        $fp = fopen("php://output", 'w');
        fwrite($fp, $output );
        fclose($fp);
    }

因此,您的代码实现了与在浏览器中打开URL相同的结果。听起来你的代码还可以,你在问如何规避反热链接保护。我闻到新鲜出炉的饼干:)我在他们的网站上没有看到关于直接下载的政策,但他们正在阻止热链接。我在考虑防止热链接。但是我用下载管理器成功地下载了这样一个文件。@user1789813你应该直接与他们联系下载,如果他们说可以,他们应该按照他们的条件发布,否则规避他们的安全可能是非法的,在美国肯定是这样。非常感谢这是解决方案!如果您想循环并创建多个文件,我刚才在底部添加的函数将有所帮助:)curl_setopt($ch,CURLOPT_REFERER,“);才是真正解决问题的方法。效果很好!@bumpaster您使用的URL可能会重定向到另一个页面。将
CURLOPT_FOLLOWLOCATION
设置为
true
以解决此问题。