PHP header function download file implementation code_PHP tutorial

WBOY
Release: 2016-07-13 17:15:17
Original
843 people have browsed it

The header function is widely used in php. The header can not only send the original HTTP header information to the client, but also directly implement the file download operation. Next, the editor will introduce it to you and welcome all friends for reference.

The header function is most commonly used not for downloading but for sending http classes

Jump

It will execute the last one, but conditionally, for example:

The code is as follows Copy code
 代码如下 复制代码

header('Location:http://www.bKjia.c0m");
header('Location:http://www.g.cn');
header('Location:http://www.baidu.com');

这个就会跳到百度

header('Location:http://www.bKjia.c0m');echo '帮客之家;
header('Location:http://www.g.cn');
header('Location:http://www.baidu.com');

header('Location:http://www.bKjia.c0m");

header('Location:http://www.g.cn');

header('Location:http://www.baidu.com');


This will jump to Baidu

header('Location:http://www.bKjia.c0m');echo 'Bang Ke Home;

header('Location:http://www.g.cn');
 代码如下 复制代码

header('HTTP/1.1 401 Unauthorized');
header('status: 401 Unauthorized');
?>

header('Location:http://www.baidu.com');

This will jump to google
 代码如下 复制代码

header('HTTP/1.1 404 Not Found');
header("status: 404 Not Found");
?>

Send status


Output the status value to the browser, mainly used for access control

The code is as follows Copy code
 代码如下 复制代码

$filename = '路径+实际文件名';
//文件的类型
header('Content-type: application/pdf');
//下载显示的名字
header('Content-Disposition: attachment; filename="保存时的文件名.pdf"');
readfile("$filename");
exit();
?>

header('HTTP/1.1 401 Unauthorized'); header('status: 401 Unauthorized'); ?>
 代码如下 复制代码
header(‘Content-type: application/octet-stream’);//输出的类型,根据下面提供的MIME表,选择相应的类型
header(‘Content-Disposition: attachment; filename=”下载显示名字.rar”‘);//下载显示的名字
readfile(‘服务器上的文件名.rar’);//
For example, if you want to restrict a user from accessing this page, you can set the status to 404, as shown below, so that the browser will display that the page does not exist
The code is as follows Copy code
header('HTTP/1.1 404 Not Found'); <🎜> header("status: 404 Not Found"); <🎜> ?>
Download Example
The code is as follows Copy code
$filename = 'path + actual file name'; <🎜> //Type of file <🎜> header('Content-type: application/pdf'); <🎜> //Download the displayed name <🎜> header('Content-Disposition: attachment; filename="File name when saving.pdf"'); <🎜> readfile("$filename"); <🎜> exit(); <🎜> ?>
Header function performs corresponding conversion,
The code is as follows Copy code
header('Content-type: application/octet -stream');//Output type, select the corresponding type according to the MIME table provided below header(‘Content-Disposition: attachment; filename=”Download display name.rar”‘);//Download display name readfile(‘File name on the server.rar’);//

要下的文件,包括路径

常用的MIME类型
.doc    application/msword
.docx   application/vnd.openxmlformats-officedocument.wordprocessingml.document
.rtf    application/rtf
.xls    application/vnd.ms-excel application/x-excel
.xlsx   application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.ppt    application/vnd.ms-powerpoint
.pptx   application/vnd.openxmlformats-officedocument.presentationml.presentation
.pps   application/vnd.ms-powerpoint
.ppsx  application/vnd.openxmlformats-officedocument.presentationml.slideshow
.pdf   application/pdf
.swf   application/x-shockwave-flash
.dll   application/x-msdownload
.exe   application/octet-stream
.msi   application/octet-stream
.chm   application/octet-stream
.cab   application/octet-stream
.ocx   application/octet-stream
.rar  application/octet-stream
.tar  application/x-tar
.tgz  application/x-compressed
.zip  application/x-zip-compressed
.z    application/x-compress
.wav   audio/wav
.wma   audio/x-ms-wma
.wmv   video/x-ms-wmv
.mp3 .mp2 .mpe .mpeg .mpg   audio/mpeg
.rm   application/vnd.rn-realmedia
.mid .midi .rmi   audio/mid
.bmp   image/bmp
.gif   image/gif
.png   image/png
.tif .tiff    image/tiff
.jpe .jpeg .jpg    image/jpeg
.txt  text/plain
.xml  text/xml
.html text/html
.css  text/css
.js   text/javascript

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/628850.htmlTechArticle在php中header函数的使用很大,header不但可以向客户端发送原始的 HTTP 报头信息,同时还可以直接实现文件下载操作,接下来小编给大家介绍...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!