开发者

How can the image and pdf files be made downloadable

Generally, browsers show the image and pdf files without embedding them in html. I need some codes to make the开发者_如何学JAVAse files not to show in the browsers but make them downloadable like doc files.

Please help me out with this.


This isn't up to you, it is up to the browser.

However, you can make a suggestion as to what to do with it by setting the content-disposition header...

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

Read the doc on the header() function: http://php.net/manual/en/function.header.php

In case this isn't clear... this is for whatever resource is returned by the PHP document. You may need a readfile() in there to do what you are trying to do.


Set a couple of headers:

$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);


<?php
header('Content-disposition: attachment; filename=myfile.pdf');
header('Content-type: application/pdf');
readfile('myfile.pdf');
?>


You want to send a content type header to make the browser download the file.

If you aren't' generating it dynamically, you will need to read it off the disk first.

$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;


try this one :

$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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜