PHP实现文件的下载实例代码
php实现文件下载我们需要用到header函数来发送相关信息给客户端浏览器,同时再结合filesize函数来读取文件大小并进行下载操作,下面我们一起来看看相关例子。
简单的文件下载只需要使用HTML的连接标记,并将属性href的URL值指定为下载的文件即可。代码如下所示:
代码如下 | 复制代码 |
如果通过上面的代码实现文件下载,只能处理一些浏览器不能默认识别的MIME类型文件,例如当访问book.rar文件时,浏览器并没有直接打开,而是弹出一个下载提示框,提示用户“下载”还是“打开”等处理方式。但如果需要下载后缀名为.html的网页文件、图片文件及PHP程序脚本文件等,使用这种连接形式,则会将文件内容直接输出到浏览器中,并不会提示用户下载。
为了提高文件的安全性,不希望在标签中给出文件的链接,则必须向浏览器发送必要的头信息,以通知浏览器将要进行下载文件的处理。PHP使用header()函数发送网页的头部信息给浏览器,该函数接收一个头信息的字符串作为参数。文件下载需要发送的头信息包括以下三部分,通过调用三次header()函数完成。以下载图片test.gif为例,需要发送的头信息的代码如下所示:
代码如下 | 复制代码 |
header(‘Content-Type:imge/gif’); //发送指定文件MIME类型的头信息 |
如果使用header()函数向浏览器发送了这三行头信息,图片test.gif就不会直接在浏览器中显示,而让浏览器将该文件形成下载的形式。在函数header()中,“Content-Type”指定了文件的MIME类型,“Content_Disposition”用于文件的描述,值“attachment; filename=”test.gif””说明这是一个附件,并且指定了下载后的文件名,“Content_Length”则给出了被下载文件的大小。
设置完头部信息以后,需要将文件的内容输出到浏览器,以便进行下载。可以使用PHP中的文件系统函数将文件内容读取出来后,直接输出给浏览器。最方便的是使用readfile()函数,将文件内容读取出来直接输出。下载文件test.gif的代码如下所示:
代码如下 | 复制代码 |
$filename = "test.gif"; header('Content-Type:image/gif'); //指定下载文件类型 header('Content-Disposition: attachment; filename="'.$filename.'"'); //指定下载文件的描述 header('Content-Length:'.filesize($filename)); //指定下载文件的大小 //将文件内容读取出来并直接输出,以便下载 readfile($filename); ?> |
上面如果碰到中文名字就会无法正常下载了,对于中文名字下载文件我又找到一个文件下载实例代码
代码如下 | 复制代码 |
header("Content-type:text/html;charset=utf-8"); |
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)可以把缓冲区内最后剩余的数据输出到磁盘文件中,并释放文件指针和有关的缓冲区

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



To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

[SpringBoot] Passing parameters in the Header through Feign calls How to pass Header parameters through Feign Problem description When we use Feign to request the Api interface of another service in Spring Cloud, there is a need to pass the parameters in the Header. If no special processing is done, it will The parameters in the Header will be lost. Solution 1: Pass it through @RequestHeader(name="headerName"). For example: Feign is defined as follows @FeignClient(name="service-name")pub

The Linux header refers to the beginning of a file or data stream, which is used to contain metadata about the content. By correctly writing and using Header files, developers can better utilize system resources and improve code readability and Maintainability.

Use java's File.getParent() function to get the parent path of a file. In Java programming, we often need to operate files and folders. Sometimes, we need to get the parent path of a file, which is the path of the folder where the file is located. Java's File class provides the getParent() method to obtain the parent path of a file or folder. The File class is Java's abstract representation of files and folders. It provides a series of methods for operating files and folders. Among them, get

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties
