1

1 つのファイルの一部を読み込もうとしています。ただし、ファイル ホスティング サーバーはヘッダーで強制ダウンロードとして返します。

fopen 、 fread を使用してファイルの一部を読み取ることができます。

$f = fopen('http://www.hdwallpapers.org/download/beautiful-red-tree-scene-1280x800.jpg','r');
$response = fread($f, 3); echo $response.'<br>';
echo $response;exit();

しかし、CURLを使用するように切り替えると

$curl = curl_init('http://www.hdwallpapers.org/download/beautiful-red-tree-scene-1280x800.jpg');
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_HTTPHEADER, $headers );

curl_setopt($curl, CURLOPT_RANGE, "0-2");
$response = curl_exec($curl);echo $response.'<br>';

それは何も返しません。

ファイルヘッダーは次のとおりです。

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Disposition:attachment; filename=beautiful-red-tree-scene-1280x800.jpg
Content-Length:427882
Content-Transfer-Encoding:binary
Content-Type:application/force-download
Date:Sat, 26 Oct 2013 04:11:25 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=75
Pragma:no-cache
Server:Apache

では、CURL で強制ダウンロード ファイルの一部を読み取るにはどうすればよいでしょうか。

4

2 に答える 2

0

次のコードを使用して試すことができます

function grab_image($url,$saveto){
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $raw=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($saveto)){
        unlink($saveto);
    }
    $fp = fopen($saveto,'x');
    fwrite($fp, $raw);
    fclose($fp);
}
于 2013-10-26T04:32:58.413 に答える