2011-02-14 4 views
0

일반적으로 브라우저는 이미지 및 pdf 파일을 HTML에 포함하지 않고 표시합니다. 이러한 파일을 브라우저에 표시하지 않고 doc 파일과 같이 다운로드 할 수있게하려면 몇 가지 코드가 필요합니다.이미지 및 pdf 파일을 다운로드 할 수있는 방법

도와주세요.

+0

아니라 .. 우리는 이미지 나 PDF 파일의 URL을 검색 할 경우, 우리는 HTML에서 그들을 포함시키지 않고 브라우저에서 파일을 볼 수 있습니다. 나는이 일이 일어나기를 원하지 않는다. 나는 그 파일을 열람 할 때 다운로드 가능하게하고 싶다. – kushalbhaktajoshi

+0

답장을 보내 주신 의견을 삭제했습니다. 아래에 많은 예제가 나와 있습니다. –

답변

0

이 하나의 시도 :

$file = 'youfile.fileextention'; 
if (file_exists($file)) { 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/octet-stream'); 
    header('Content-Disposition: attachment; filename='.basename($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); 
    exit; 
+0

나는 더 나은 Content-Type이 application/force-download가 될 것이라고 믿는다. octet-stream은 실행 파일 용으로 사용됩니다. – Jacob

5

이것은 귀하에게 달려있는 것은 아니며 브라우저에 달려 있습니다. 경우 http://php.net/manual/en/function.header.php

이 ISN :

그러나

header("Content-Disposition: attachment; filename=\"yourfilename.pdf\""); 

header() 기능에 문서를 ... 읽기 content-disposition 헤더를 설정하여 그것으로 무엇을 해야할지에 관한 제안을 할 수 있습니다 't clear ... 이것은 PHP 문서에서 반환되는 모든 리소스에 대한 것입니다. 당신이하려고하는 것을하기 위해 거기에 readfile()이 필요할 수도 있습니다.

2

설정 헤더의 몇 :

$filename = ...; 
$mime_type = ...; //whichever applicable MIME type 
header('Pragma: public'); 
header('Expires: 0'); 
header("Content-Disposition: attachment; filename=\"$filename\""); 
header("Content-Type: $mime_type"); 
header('Content-Length: ' . filesize($filename)); 
readfile($filename); 
1
<?php 
header('Content-disposition: attachment; filename=myfile.pdf'); 
header('Content-type: application/pdf'); 
readfile('myfile.pdf'); 
?> 
1

당신은 브라우저가 파일을 다운로드 할 콘텐츠 형식 헤더를 보내려고합니다.

동적으로 생성하지 않으면 먼저 디스크에서 읽어야합니다.

$fullPath = "/path/to/file/on/server.pdf"; 
$fsize = filesize($fullPath); 
$content = file_get_contents($fullPath); 
header("Pragma: public"); // required 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: application/pdf"); 
header("Content-Disposition: attachment; filename=\"".basename($fullPath)."\";"); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".$fsize); 
echo $content; 
관련 문제