10

我尝试使用 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);

另一个奇怪的事情是在网络浏览器中输入 URL 时,我没有得到文件。我只能在单击网站上的链接时下载文件!。

任何建议都非常感谢!

4

3 回答 3

27

试一试

<?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
?>
于 2012-10-31T22:37:15.153 回答
2

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

来源:https ://www.kodingmadesimple.com/2018/02/php-download-file-from-url-curl.html

于 2020-11-05T21:31:05.263 回答
0

在我这样做之前,我在使用 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);
    }
于 2020-11-15T14:03:19.200 回答