10

次のようなURLからphp-scriptを使用してファイルをダウンロードしようとしています。

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);

もう1つの奇妙なことは、WebブラウザにURLを入力すると、ファイルが取得されないことです。Webサイトのリンクをクリックしたときにのみファイルをダウンロードできました!。

どんなアドバイスも大歓迎です!

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 に答える