문제

다운로드를 스트리밍하여 서버를 통해 파일을 다운로드하려고합니다.

이것은 내 대본입니다.

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=http://user:pass@example.com/file.bin';
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $r[2]);
ob_clean();
flush();
readfile($fileName);
exit;

example.com은 내가 실제로 사용하는 것이 아니라 실제 URL을 보여주고 싶지 않았습니다. :) 내 웹 브라우저 에서이 파일로 이동하면 다운로드 프롬프트가 나타나지 않으며 아무 일도 일어나지 않습니다. $r[2] 내용 길이는 파일의 바이트이며 $fileName 부착 헤더에 사용 된 것과 동일한 URL입니다. 내가 뭘 잘못하고 있는지 잘 모르겠습니다.

도움이 되었습니까?

해결책

readfile (); URL이 아닌 컴퓨터의 파일의 절대 경로가 필요합니다.

예 : /home/person/file.exe

다른 팁

누락이 있습니다 ) 파일 이름 헤더 라인의 끝에서는 라인을 편집했을 때 발생했다고 가정합니다.

가치가있는 것에 대해, 샘플 코드를 실행하면 다운로드 프롬프트가 제공되며 실제로 그 과거를 테스트하지는 않았지만 코드가 아닌 서버/클라이언트 구성이있는 것일 수 있습니다.

IMHO 문제는 파일 크기입니다. HTTP를 통해 파일 크기를 얻으십시오 (이 기능을보십시오).http://codesnippets.joyent.com/posts/show/1214

그래서 편집

header('Content-Length: ' . $r[2]);

에게

header('Content-Length: ' . get_remote_file_size($fileurl) );

문제를 해결하기를 바랍니다.

예를 들어 이미지를 다운로드하는 방법을 찾았지만 모든 종류의 파일에 도움이 될 수 있습니다. 사용하지 않고 파일을 다운로드합니다 readfile.

나는 그것이 어색 할 수 있다는 것을 알고 있지만 때로는 작업을 완료하기 위해서는 해결 방법이 필요합니다.

이것을 어디에서 시도하십시오 $src = 'http://external-server/remote-image.jpg'

$url_stuff = parse_url($src); 
$port = isset($url_stuff['port']) ? $url_stuff['port'] : 80; 
$fp = fsockopen($url_stuff['host'], $port); 
$query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n"; 
$query .= 'Host: ' . $url_stuff['host']; 
$query .= "\n\n"; 
$buffer=null;
fwrite($fp, $query); 
while ($tmp = fread($fp, 1024)) 
{ 
    $buffer .= $tmp; 
} 

preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts); 
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=your_file.bin');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

$content = substr($buffer, - $parts[1]); 
echo $content;

이것은 테스트되고 작동합니다 ;-)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top