通过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 Densin.Tian@CisternData 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

AI Hentai Generator
Generate AI Hentai for free.

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

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio
