解析PHP实现下载文件的两种方法

yipeiwu_com6年前PHP代码库
方法一:
复制代码 代码如下:

 header('Content-Description: File Transfer');
 header('Content-Type: application/octet-stream');
 header('Content-Disposition: attachment; filename='.basename($filepath));
 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($filepath));
 readfile($file_path);

方法二:
复制代码 代码如下:

 $fileinfo = pathinfo($filename);
 header('Content-type: application/x-'.$fileinfo['extension']);
 header('Content-Disposition: attachment; filename='.$fileinfo['basename']);
 header('Content-Length: '.filesize($filename));
 readfile($thefile);
 exit();

相关文章

PHP 命令行工具 shell_exec, exec, passthru, system详细使用介绍

PHP 命令行工具 shell_exec, exec, passthru, system详细使用介绍

所有这些命令都衍生一个子进程,用于运行您指定的命令或脚本,并且每个子进程会在命令输出写到标准输出 (stdout) 时捕捉它们。 shell_exec() shell_exec() 命令...

PHP临时文件的安全性分析

一、简介   临时文件,顾名思义是临时产生的文件,且文件的生命周期很短。   然而,很多应用的运行都离不开临时文件,临时文件在我们电脑上无处不在,主要有以下几种形式的临时文件: 1.文件...

php对csv文件的读取,写入,输出下载操作详解

复制代码 代码如下:<?php       $file = fopen('text.csv','r');   ...

PHP 使用二进制保存用户状态的实例

前言 用户状态保存是一个很常见的需求,一般用来保存用户状态的方式是在数据库表中创建多个字段来存储相应的用户状态,比如要保存用户是否绑定了手机号和QQ,则需要2个字段(mobile,qq)...

PHP解压tar.gz格式文件的方法

PHP解压tar.gz格式文件的方法

本文实例讲述了PHP解压tar.gz格式文件的方法。分享给大家供大家参考,具体如下: 1、运用php自带压缩与归档扩展(phar) $phar = new PharData('son...