开发者

Force file download in php

I have built a simple file manager where users can download any type of file such as pdf, word or gif files. I want all of them to download file rather than view it in brows开发者_运维百科ers. The uploaded filenames are stored in database.


<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?> 

http://php.net/manual/en/function.header.php


You can use the "Content-Disposition" header for that:

header("Content-Disposition: attachment");

The PHP manual provides an excellent example for that.


Normally setting the Content-Disposition to attachment before sending a file force a download by the browser.

You either need to configure your web server to provide this header for the files or send them yourself via PHP, sending a specific header before like :

header('Content-Disposition: attachment; filename=your_file_name.pdf');

Beware that the first solution is better as you won't risk your downloads being cut because the script running time is too long (you could also alter it).


header("Expires: Mon, 26 Jul 1997 05:00:00 GMT\n");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-type: application/pdf;\n");
$len = filesize($filename);
header("Content-Length: $len;\n");
header("Content-Disposition: attachment; filename=\"downfile.pdf\";\n\n");
echo readfile($filename)


source code taken from the TCPDF library

            // download PDF as file
            if (ob_get_contents()) {
                $this->Error('Some data has already been output, can\'t send PDF file');
            }
            header('Content-Description: File Transfer');
            if (headers_sent()) {
                $this->Error('Some data has already been output to browser, can\'t send PDF file');
            }
            header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
            header('Pragma: public');
            header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
            header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
            // force download dialog
            if (strpos(php_sapi_name(), 'cgi') === false) {
                header('Content-Type: application/force-download');
                header('Content-Type: application/octet-stream', false);
                header('Content-Type: application/download', false);
                header('Content-Type: application/pdf', false);
            } else {
                header('Content-Type: application/pdf');
            }
            // use the Content-Disposition header to supply a recommended filename
            header('Content-Disposition: attachment; filename="'.basename($name).'";');
            header('Content-Transfer-Encoding: binary');
            $this->sendOutputData($this->getBuffer(), $this->bufferlen);
            break;

anyways the most important part is

 header('Content-Disposition: attachment; filename="'.basename($name).'";');

and notice that final ; inside the string, without it, it won't work

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜