2014-04-02 4 views
0

사용자가 링크를 클릭하면 파일을 강제로 다운로드하려고합니다. 링크를 클릭하면 압축 파일이 즉시 만들어져 사용자 컴퓨터에 다운로드됩니다. 내 코드는 localhost에서 실행될 때 작동하지만 서버에서 실행할 때 빈 페이지를 표시합니다. 파일을 다운로드하려고 할 때 빈 페이지가 나타납니다.

파일을 다운로드 링크입니다 :

<a href="downloadfile.php"> Download </a> 

그리고 이것은 downloadfile.php입니다 : 프로덕션 서버에서 사용 ziparchive 경우

<?php 

     $file = tempnam("tmp", "zip"); 
     $zip = new ZipArchive(); 
     $zip->open($file, ZipArchive::OVERWRITE); 
     $zip->addFromString('test.txt', "this is a test file"); 
     $zip->close(); 

     header('Content-Description: File Transfer'); 
     header('Content-Type: application/octet-stream'); 
     header('Content-Disposition: attachment; filename="testfile.zip" '); 
     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(); 
?> 
+0

서버 응답의 상태 코드는 무엇의 상단에이 줄을 추가하려고하기 때문에 다른 문제가 있다고 생각? –

+0

코드가 서버에서 실행되고 인스턴스 리디렉션 문제가 없는지 확인 했습니까? – Bowdzone

+0

제작중인 경우 php.ini가 해당 지역과 다른 것으로 생각됩니다. 오류가 던졌습니다 (빈 페이지) 같아요. 아파치 로그를 살펴 봐야합니다. – Debflav

답변

2

가 확인하시기 바랍니다. 이에

header('Content-Type: application/octet-stream'); 

:

header("Content-Type: application/zip"); 

당신이 올바른 헤더를 사용한다

또한이 헤더를 변경합니다. 내 로컬 컴퓨터에서이 테스트를 거쳤습니다.

+0

phpinfo를 실행할 때 "zip"을 찾을 수 없습니다. 이것이 문제라고 생각하십니까? – user3309825

+0

예, 사용중인 PHP 버전 및 OS는 무엇입니까? – Perry

+0

5.1.6입니다. 나는 macOS를 사용한다. 해결 방법에 대해 알고 있습니까? 이 시점에서 서버를 최신 버전으로 업데이트 할 수 없습니다. – user3309825

0

내가 스크립트 내 서버에 작업 스크립트 페이지

ob_start(); 
관련 문제