search
Homephp教程php手册图解PHP文件下载原理实例

有时为了安全及用户权限,我们不会直接把文件存在web路径用http给客户端下载,现在我们来讲讲如何php实现文件下载的原理及实例。

1、php下载原理图

图解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 . &#39;/&#39; . 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(&#39;http://news.sina.com.cn/c/2011-09-16/021323162600.shtml&#39;);


文章地址:

转载随意^^请带上本文地址!

Statement
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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.