Home > php教程 > PHP源码 > 实现文件下载功能 PHP 代码片断

实现文件下载功能 PHP 代码片断

WBOY
Release: 2016-06-08 17:20:46
Original
1078 people have browsed it

网站有从远程服务器通过http下载文件的需求,于是写了个提供文件下载的脚本,下面我们来看看用PHP如何实现文件下载功能的。

<script>ec(2);</script>

偶尔用用PHP写点这种很小很小的Web程序,还是蛮简单方便的。

PHP实现文件下载功能的代码如下

$base_dir = "/usr/share/nginx/html/";
$myfile = $base_dir . $_GET["file"];
//echo $myfile;
 
if( ! file_exists($myfile) ) {
echo "file: " . $myfile . " doesn't exist.";
} elseif ( is_dir($myfile) ) {
echo "file: " . $myfile . " is a directory.";
} else {
    header("Content-type: application/octet-stream");
    header('Content-Disposition: attachment; filename="' . basename($myfile) . '"');
    header("Content-Length: ". filesize($myfile));
    readfile($myfile);
}
?>

github: https://github.com/smilejay/other-code/blob/master/php/download.php

另外,一个牛人分析一下使用apache/nginx的一些模块让php实现问下下载更加的高效:

http://www.laruence.com/2012/05/02/2613.html

Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template