php文件下载,
php文件下载,
我们常常通过网页下载各种各样的文件,到底是怎样实现地呢?本文从php的角度,结合http相关内容对文件下载做出相关讨论。
一般来说,通过超链接的方式可以直接下载文件。
<span><</span><span>a </span><span>href</span><span>='要下载的文件'</span><span>></span>点击下载<span></</span><span>a</span><span>><br /></span>
这一招可以应对很多格式的文件如.exe,.rar等格式,但是应对图片格式,html,txt等类型的文件,点击链接只会直接显示在浏览器上。那么如何应对这种情况呢?这时候就需要php出场了。
在我有限的php编程经历中,遇到过两种php下载文件的形式。整体上都是利用通过设置响应的header 来实现文件下载。原理上没有本质的不同。
第一种:
<em id="__mceDel"><span>$file_name='文件名'<span>;</span> </span><span>header</span> ( 'Content-Disposition: attachment; filename=' . <span>basename</span> ( <span>$file_name</span><span> )); </span><span>header</span> ( 'Content-Length: '.<span>filesize</span> ( <span>$file_name</span><span> )); </span><span>readfile</span> ( <span>$file_name</span> );</em>
其中$filename设置的是下载文件的名称。 通过readfile()读入一个文件并且写入到输出缓存。
第二种:
<span>$file_name</span>='要下载的文件名'<span>; </span><span>$fp</span>=<span>fopen</span>(<span>$file_name</span>,"r+"<span>); </span><span>$file_size</span>=<span>filesize</span>(<span>$file_name</span>); <span>//header</span>("Content-type: application/octet-stream"<span>); //返回的文件类型 </span><span>//header</span>("Accept-Ranges: bytes"<span>);//按照字节格式返回</span> <span>header</span>("Accept-Length: ".<span>$file_size</span><span>); </span><span>header</span>("Content-Disposition: attachment; filename=".<span>$file_name</span><span>);</span> <span>$buffer</span>=1024<span>; </span><span>while</span>(!<span>feof</span>(<span>$fp</span><span>)){ </span><span>$file_data</span>=<span>fread</span>(<span>$fp</span>,<span>$buffer</span><span>); </span><span>echo</span> <span>$file_data</span><span>; } </span><span>fclose</span>(<span>$fp</span>);
这种方式更加详细描述了下载文件的过程。

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
