透过PHP将文件以流的方式发送客户端
通过PHP将文件以流的方式发送客户端
以前在ASP下,通过BASP21实现类似功能。现在将PHP版本整理在这里。
通过以下代码中所使用的技术可以实现如下几个可能的需求
1、在有模板的基础上,针对客户的定制下载。
2、一些浏览器本身能够表示的文件类型(如txt文件),可以通过将头信息Content-Type改为 header("Content-Type: application/force-download");进行强制下载。
3、对于一些动态生成文件,比如TXT/CSV等文件,将临时文件及时删除
<?php /** * 本程序实现了将测试文件[test.pdf]创建副本,并将副本读入 * 内存,以流的方式发送给客户端浏览器,最终删除副本的操作。 * * 本文可以实现的需求: * 1、在有模板的基础上,针对客户的定制下载。 * 2、一些浏览器本身能够表示的文件类型(如txt文件),可以通过将头信息Content-Type * 改为 header("Content-Type: application/force-download");进行强制下载。 * 3、对于一些动态生成文件,比如TXT/CSV等文件,可以将临时文件及时删除 * @author [email protected] 2015/05/05 */// 文件名扩展名$fileExName = ".pdf";// 原始文件文件名$oldFileMainName = "testfile";// 获得时间戳 YYYYMMDDHHMISS$timestamp = date ( "YmdHis", time () );// 新文件名$newFileName = $oldFileMainName . $timestamp;// 复制文件if (! copy ( $oldFileMainName . $fileExName, $newFileName . $fileExName )) { die ( "failed to copy" );}// 设置下载头信息header ( "Content-Type: application/pdf" );header ( "Content-Disposition: attachment; filename=20150505.pdf" );// 文件流输出到浏览器readfile ( $newFileName . $fileExName );// 删除文件@unlink ( $newFileFullName );?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The python environment this article relies on is: What is WSGI? WSGI is also called the web server universal gateway interface, and its full name is webservergatewayinterface. It defines a standard for how web servers and web applications should communicate and handle http requests and responses in Python. Note that it is just a protocol, or a specification or standard. You don’t have to follow this standard. Just like the web server we wrote in the previous article. WSGI is also divided into applications and server gateways. Among them, the well-known Flask belongs to applications, and uWSGI and wsgiref belong to server gateways. Personal feeling, WSG

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

The difference between bootstrap and application explains that both bootstrap and application are configuration files in the SpringBoot project. Their differences mainly include the following aspects (1) Loading order difference. The bootstrap configuration file is loaded first than the application configuration file, because bootstrap is composed of The spring parent context is loaded, and the application is loaded by the child context (2) Priority difference The configuration information loaded by bootstrap cannot be overwritten by the same configuration of the application. If two configuration files exist at the same time, bootstrap will be the main one (

To protect against malicious input, applications implement a number of security mechanisms that are conceptually similar. These security mechanisms consist of the following aspects: 1. Processing the data and functions of users accessing web applications (preventing unauthorized access) 2. Processing data input by users to web application functions (preventing the construction of malicious data) 3. Responding to attacks (Handling unexpected errors, automatically blocking obvious attacks, automatically sending alerts to administrators, and maintaining program access logs) 4. Management and maintenance of application processing access. Usually there are different types of users for an application, such as ordinary users, logins Authenticate users, administrators. Different permissions are given to different user web applications so that they can only access different data and functions. w

Detailed explanation of CSS content attributes: content, counter and quotesCSS (cascading style sheets) is an integral part of front-end development. It can help us beautify web pages and enhance user experience. In CSS, there are some special properties that can be used to control the display of text content, including content, counter, and quotes. This article explains these properties in detail and provides specific code examples. 1. content attribute content attribute

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没问题。

图片消失如何解决先是图片文件上传$file=$_FILES['userfile']; if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

图片消失如何解决先是图片文件上传$file=$_FILES['userfile']; if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'
