2

PHPを使用してダウンロードリダイレクトを作成しようとしています。葯サーバーからファイルを取得しています

例: http: //rarlab.com/rar/wrar420.exe

スクリプトがファイルを一時変数として保存し、同時に保存すると同時に、ダウンロードとしてブラウザに送信します...

function download($url,$name,$hash){ 
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $data = curl_exec($ch); 
        curl_close($ch);
        header('Content-Description: File Transfer');
        header("Content-Disposition: attachment; filename=".$name);  
        ob_clean();
        flush();
        readfile($url);
}
download("http://rarlab.com/rar/wrar420.exe","winrar.rar","26digitHASH");

THX君たち。

4

1 に答える 1

1

あなたがやろうとしていることは、PHP が作成された範囲からほとんど外れていますが、cURL 実装のマルチハンドル機能のおかげで、それが可能になるはずです。

マルチハンドル機能の使用方法については、 curl_multi_exec()のドキュメントを参照してください。

この回答によると、転送が完了する前にcurl_multi_getcontent()を呼び出すことができます:

//execute the handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }

    // echo the contents downloaded so far
    // Note that this must be called with the curl handle, not with the multi handle.
    echo curl_multi_getcontent($ch);
    flush();

}
于 2012-11-20T21:30:59.610 に答える