2013-11-03 2 views
0

내가 시도에 다음 코드는이 때문에 서버의 특정 파일을 다운로드하도록 한 대신 서버에서 (PHP)를 하나를 전송하는 새로운 파일을 생성 그것은 브라우저가 내 케이스 "Order.txt"에서 파일을 다운로드하게하지만, 그 공란은 그냥 어디서나이 파일을 생성하는 것입니다. 그것은 $ file의 "upload /"경로 부분으로는 아무 것도하지 않습니다.헤더 파일 다운로드는

어떤 도움을 주셔서 감사합니다. 헤더가 정말로 혼란스럽고, 왜 "다운로드 ($ 파일)"을 가질 수 없습니까?

편집 : 며칠 동안 문제를 해결하기 위해 마침내 내 자신을 고친 후 마침내 여기에 물어보기로 결심합니다.

파일의 위치가 PHP 스크립트와 동일하게 변경되었지만 여전히 별도로 작동하는 방법을 알고 싶습니다. 나는 또한 다음과 같이 덧붙였다.

readfile($file); 

나는이 2 가지 변경 사항을 변경하면 작동하지 않는다.

은 또한 슬림이 줄 지어, 새로운 코드 : 헤더 후 stdout에

header("Content-Disposition: attachment; filename='".basename($file)."';"); 
+0

가능한 중복 [php, 파일 다운로드] (http://stackoverflow.com/questions/5595485/php-file-download) –

+1

당신은 1 개의'Content-Type '우두머리, 몇몇을 보내는 것은 무용하다. –

답변

0

이 줄을 변경 시도

$file = 'upload/Order.txt'; 
header("Pragma: public", true); 
header("Expires: 0"); // set expiration time 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download"); 
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); 
header("Content-Disposition: attachment; filename=".basename($file)); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".filesize($file)); 
$handle = fopen($file, "r");   
$contents = fread($handle, filesize($file)); 
fclose($handle); 
echo $contents; 
+0

'filename ='인용 부호로 묶어야합니다. 'filename = ""' –

+0

죄송합니다, 감사합니다. – Legionar

+0

작은 따옴표를 사용하면이 행을 훨씬 쉽게 읽을 수 있습니다. –

0

당신이 파일의 내용을 인쇄한다 :

$file = 'Order.txt'; 
header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header("Content-Disposition: attachment; filename=$file"); 
header("Content-Type: application/zip"); 
header("Content-Transfer-Encoding: binary"); 
readfile($file); 
+0

['readfile'] (http://php.net/readfile)을 사용하지 않는 이유는 무엇입니까? –

+0

작은 파일 (큰 파일의 경우 메모리를 초과하지 않도록 부분적으로 읽을 수 있음) – ziollek

+0

BTW는'Content-Length' 헤더를 보내지 않아도 필요하지 않으며 버그를 생성하기 쉽습니다. –