图解PHP文件下载原理实例
有时为了安全及用户权限,我们不会直接把文件存在web路径用http给客户端下载,现在我们来讲讲如何php实现文件下载的原理及实例。
1、php下载原理图
2、文件下载源码:
<?php $file_name = "哈哈.jpg"; //需要下载的文件 $file_name = iconv("utf-8", "gb2312", "$file_name"); $fp = fopen($file_name, "r+"); //下载文件必须先要将文件打开,写入内存 if (!file_exists($file_name)) { //判断文件是否存在 echo "文件不存在"; exit(); } $file_size = filesize("a.jpg"); //判断文件大小 //返回的文件 Header("Content-type: application/octet-stream"); //按照字节格式返回 Header("Accept-Ranges: bytes"); //返回文件大小 Header("Accept-Length: " . $file_size); //弹出客户端对话框,对应的文件名 Header("Content-Disposition: attachment; filename=" . $file_name); //防止服务器瞬时压力增大,分段读取 $buffer = 1024; while (!feof($fp)) { $file_data = fread($fp, $buffer); echo $file_data; } //关闭文件 fclose($fp); ?>
3、文件编码问题解决方法:
如果文件名是中文,php的函数不能识别中文文件名,一般如果程序编码为utf-8,php的函数比较古老,只能识别gb2312编码的中文,所以把中文用iconv(“原编码”,”要转成的编码”,”要转码的字符串”)函数可以转码。
比如,把一个字符串从utf-8转码为gb2312
$file_name=iconv(“utf-8”,”gb2312”,”$file_name”);
PHP下载远程文件原理
<?php /** * PHP下载远程文件到本地原理:通过PHP函数,先读取远程文件,然后在本地创建一个新的空文件, * 然后将已读取的远程文件的内容写入到新创建的文件当中,这样就可以达到远程文件下载功能 * @author JackyLi * */ class DownloadFile { /** * @param string $file 远程需要下载的文件 */ public static function get_img_file($file) { $targetDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "plupload" . DIRECTORY_SEPARATOR; if (!file_exists($targetDir)) { //检测临时下载目录是否存在,不存在,则新建一个 @mkdir($targetDir); } if (!$file) { return false; } $ext = strrchr($file, "."); //取扩展名 $filename = $targetDir . '/' . basename($file, $ext) . date("YmdHis") . $ext; //需要保存的文件名称(带完整路径) ob_start(); //开启output buffering readfile($file); //将文件读取buffering中 $img = ob_get_contents(); //将buffering中的数据保存到变量当中,方便后续操作 ob_end_clean(); //关闭output buffering $fp2 = @fopen($filename, "a"); //打开目标文件(马上被写入数据的文件) fwrite($fp2, $img); //写入数据到文件当中 fclose($fp2); //关闭文件句柄 //上面读取文件内容,可以直接用下面两行代替 // $file = file_get_contents($file); // file_put_contents($filename,$file); return true; } } $download = new DownloadFile(); $download->get_img_file('http://news.sina.com.cn/c/2011-09-16/021323162600.shtml');
文章地址:
转载随意^^请带上本文地址!

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



Python provides the following options to open downloaded files: open() function: open the file using the specified path and mode (such as 'r', 'w', 'a'). Requests library: Use its download() method to automatically assign a name and open the file directly. Pathlib library: Use write_bytes() and read_text() methods to write and read file contents.

To implement file upload and download in Workerman documents, specific code examples are required. Introduction: Workerman is a high-performance PHP asynchronous network communication framework that is simple, efficient, and easy to use. In actual development, file uploading and downloading are common functional requirements. This article will introduce how to use the Workerman framework to implement file uploading and downloading, and give specific code examples. 1. File upload: File upload refers to the operation of transferring files on the local computer to the server. The following is used

How to use Laravel to implement file upload and download functions Laravel is a popular PHP Web framework that provides a wealth of functions and tools to make developing Web applications easier and more efficient. One of the commonly used functions is file upload and download. This article will introduce how to use Laravel to implement file upload and download functions, and provide specific code examples. File upload File upload refers to uploading local files to the server for storage. In Laravel we can use file upload

How to use PHP functions to upload and download attachments for sending and receiving emails? With the rapid development of modern communication technology, email has become an important way for people to communicate and transmit information in daily life. In web development, we often encounter the need to send and receive emails with attachments. As a powerful server-side scripting language, PHP provides a wealth of functions and class libraries that can simplify the email processing process. This article will introduce how to use PHP functions to upload and download attachments for sending and receiving emails. Email is sent first, we

How to solve PHPWarning:fopen():SSLoperationfailedinfile.phponlineX In PHP programming, we often use the fopen function to open files or URLs and perform related operations. However, when using the fopen function, sometimes you will encounter something similar to Warning:fopen():SSLoperationfailedinfile.p

How to solve PHPWarning:fopen():failedtoopenstream:Permissiondenied In the process of developing PHP programs, we often encounter some error messages, such as PHPWarning:fopen():failedtoopenstream:Permissiondenied. This error is usually due to incorrect file or directory permissions

How to solve PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory In the process of using PHP development, we often encounter some file operation problems, one of which is "PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory"

How to use the Hyperf framework for file downloading Introduction: File downloading is a common requirement when developing web applications using the Hyperf framework. This article will introduce how to use the Hyperf framework for file downloading, including specific code examples. 1. Preparation Before starting, make sure you have installed the Hyperf framework and successfully created a Hyperf application. 2. Create a file download controller First, we need to create a controller to handle file download requests. Open the terminal and enter
