2010-04-26 8 views
1

또는 서버에 파일을 저장하지 않고 파일을 다운로드 할 수 있습니까? 데이터베이스에서 데이터를 가져 오는 중 .doc (MS Word) 파일을 저장하려고합니다.서버에 저장하지 않고 파일을 다운로드하십시오.

if(isset($_POST['save'])) 
{ 
$file = "new_file2.doc"; 
$stringData = "Text text text text...."; //This data put in new Word file 

$fh = fopen($file, 'w');  
fwrite($fh, $stringData); 
fclose($fh); 

header('Content-Description: File Transfer'); 
header('Content-type: application/msword'); 
header('Content-Disposition: attachment; filename="'.$file.'"'); 
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: ' . filesize($file)); 
ob_clean(); 
flush(); 
readfile($file);  
unlink($file); 
exit; 

}

어떻게해야 같은 코드 모양이없이 : "$ FH =하면 fopen ($ 파일, 'w');
에 fwrite ($ FH, $ StringData가) FCLOSE ($ fh); " 및이 "unlink ($ file);"

은 내가 다음의 내용을 출력 헤더에 여기

답변

5
당신은 단지 필요

코드를 입력하고 필요한 것을 이해하기 바란다 :

header(...); 

echo $stringData; 

필요가있는 파일을 재생할 수 없습니다.

0
header("Content-type: application/octet-stream");  
header("Content-Disposition: attachment; 
filename=\"$_fileName\""); 
ob_clean(); 
readfile($_filePath); 
관련 문제