Home > Backend Development > PHP Tutorial > PHP下载文件(隐藏真实的下载地址)

PHP下载文件(隐藏真实的下载地址)

WBOY
Release: 2016-06-20 12:49:30
Original
1231 people have browsed it

        

有时我们需要隐藏真实的文件下载地址,防止恶意下载,我们可以采用下面的方法(参考网络,侵权请告知)


以下为引用的内容:

$file_name = "example.exe";

$file_dir = "/public/www/download/";

if (!file_exists($file_dir . $file_name)) { //检查文件是否存在

        echo "文件未找到";

        exit; 

} else {

        $file = fopen($file_dir . $file_name,"r"); // 打开文件 

        // 输入文件标签

        Header("Content-type: application/octet-stream");

        Header("Accept-Ranges: bytes");

        Header("Accept-Length: ".filesize($file_dir . $file_name));

        Header("Content-Disposition: attachment; filename=" . $file_name);

        // 输出文件内容

        echo fread($file,filesize($file_dir . $file_name));

        fclose($file);

        exit;

}


而如果文件为远程文件,则原始码会有少许改动,程式如下:


以下为引用的内容:

$file_name = "example.exe";

$file_dir = "http://back.zhizhi123.com/"; 

$file = @ fopen($file_dir . $file_name,"r"); 

if (!$file) {

    echo "文件未找到";

} else {

    Header("Content-type: application/octet-stream");

    Header("Content-Disposition: attachment; filename=" . $file_name);

    while (!feof ($file)) {

    echo fread($file,50000);

}

fclose ($file);

}

这样就能用PHP直接输出文件了。 


source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template