2014-07-15 4 views
0

나는 다음과 같은 PHP 코드가 있습니다콘텐츠 유형 헤더를 설정하기 전에 PHP가 에코됩니까?

function download_file($file, $description=null, $filename=false){ 
    $filename = $filename || basename($file); 
    ob_start(); 
    if(is_string($description)){ 
     $description = preg_replace("/\$1/", $filename, $description); 
     echo $description; 
    } 
    sleep(2); 
    header('Content-Type: '.$this->file_mimetype($file)); 
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-disposition: attachment; filename=\"" . basename($file) . "\""); 
    readfile($file); 
    header("Content-Type: text/html"); 
    ob_end_flush(); 
} 
download_file("https://raw.githubusercontent.com/Gethis/ED/master/easydevop.class.php", "Downloading easydevop.class.php"); 

문제는 그것을 다운로드하기 전에 "easydevop.class.php 다운로드"반향되지 않도록이다. 나는 또한 모든 머리글 뒤에 그것을 울리는 시도했지만, 그 중 하나를 작동하지 않았다. 제발, 도와주세요. 당신이 볼 수 있듯이

나는 당신이 "에코"(보여주는 HTML 컨텐츠)를 사용하고 동시에 사용자에게 파일을 보낼 수 없습니다 ob_start()ob_end_flush()

답변

1

을 사용했다. 당신은 사용하여 파일에 사용자를 리디렉션 먼저 HTML 페이지를 보여줄 수

<META HTTP-EQUIV="REFRESH" CONTENT="0;URL=http://url.to/file/or_script/that_send_file/"> 

을 HTML을 리디렉션 또는 I가 이미 파일을 다운로드 할 때 에코를 표시 할 수 없습니다 언급 한 바와 같이 자바 스크립트가 How do I redirect with Javascript?

+0

또는 헤더 리디렉션 :'헤더 다음은 테스트 스크립트 ('새로 고침 : HTTP는 : // ...');' – Marek

+0

또는 위치 : 헤더 – Farkie

0

를 리디렉션합니다. 파일을 다운로드하면 파일을 다운로드 할 수 있습니다.

그러나 JavaScript를 사용하면 다운로드를 시작하기 전에 메시지를 표시 할 수 있습니다.

<?php 
if (isset($_GET['id'])) { 
    $file = 'testfile.txt'; 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='.basename($file)); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize($file)); 
    readfile($file); 
    exit;   
} 
?> 

<!DOCTYPE html> 
<html> 
<head> 
     <meta charset="utf-8" /> 
</head> 
<body> 

    <script type="text/javascript"> 
    function showDownload(message) { 
     document.getElementById("hidden").innerHTML = message; 
     document.getElementById("link1").style.display = 'none'; // you can even hide download link if you want 
    } 
    </script> 

<div id="hidden"></div> 
<a href="download.php?id=1" onclick="showDownload('You are downloading file id = 1'); return true;" id="link1">Download</a> 
</body> 
</html> 
관련 문제