Home php教程 php手册 php实现文件下载代码分享

php实现文件下载代码分享

Jun 06, 2016 pm 08:19 PM
php Download Document

php实现文件下载我们需要用到header函数来发送相关信息给客户端浏览器,同时再结合filesize函数来读取文件大小并进行下载操作,下面我们一起来看看相关例子。

简单的文件下载只需要使用HTML的连接标记,并将属性href的URL值指定为下载的文件即可。所示:

下载文件

如果通过上面的代码实现文件下载,只能处理一些浏览器不能默认识别的MIME类型文件,例如当访问book.rar文件时,浏览器并没有直接打开,而是弹出一个下载提示框,提示用户“下载”还是“打开”等处理方式。但如果需要下载后缀名为.html的网页文件、图片文件及PHP程序脚本文件等,使用这种连接形式,则会将文件内容直接输出到浏览器中,并不会提示用户下载。

为了提高文件的安全性,不希望在标签中给出文件的链接,则必须向浏览器发送必要的头信息,以通知浏览器将要进行下载文件的处理。PHP使用header()函数发送网页的头部信息给浏览器,该函数接收一个头信息的字符串作为参数。文件下载需要发送的头信息包括以下三部分,通过调用三次header()函数完成。以下载图片test.gif为例,需要发送的头信息的所示:

header(‘Content-Type:imge/gif'); //发送指定文件MIME类型的头信息 header(‘Content-Disposition:attachment; filename=”test.gif”‘); //发送描述文件的头信息,附件和文件名 header(‘Content-Length:3390′); //发送指定文件大小的信息,单位字节

如果使用header()函数向浏览器发送了这三行头信息,图片test.gif就不会直接在浏览器中显示,而让浏览器将该文件形成下载的形式。在函数header()中,“Content-Type”指定了文件的MIME类型,“Content_Disposition”用于文件的描述,值“attachment; filename=”test.gif””说明这是一个附件,并且指定了下载后的文件名,“Content_Length”则给出了被下载文件的大小。

设置完头部信息以后,需要将文件的内容输出到浏览器,以便进行下载。可以使用PHP中的文件系统函数将文件内容读取出来后,直接输出给浏览器。最方便的是使用readfile()函数,将文件内容读取出来直接输出。下载文件test.gif的所示:

上面如果碰到中文名字就会无法正常下载了,对于中文名字下载文件我又找到一个文件下载实例代码
 

header("Content-type:text/html;charset=utf-8")的作用:在服务器响应浏览器的请求时,,告诉浏览器以编码格式为UTF-8的编码显示该内容

关于file_exists()函数不支持中文路径的问题:因为php函数比较早,不支持中文,所以如果被下载的文件名是中文的话,需要对其进行字符编码转换,否则file_exists()函数不能识别,可以使用iconv()函数进行编码转换

$file_sub_path() 我使用的是绝对路径,执行效率要比相对路径高

Header("Content-type: application/octet-stream")的作用:通过这句代码客户端浏览器就能知道服务端返回的文件形式 Header("Accept-Ranges: bytes")的作用:告诉客户端浏览器返回的文件大小是按照字节进行计算的 Header("Accept-Length:".$file_size)的作用:告诉浏览器返回的文件大小 Header("Content-Disposition: attachment; filename=".$file_name)的作用:告诉浏览器返回的文件的名称

以上四个Header()是必需的
fclose($fp)可以把缓冲区内最后剩余的数据输出到磁盘文件中,并释放文件指针和有关的缓冲区

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles