>  기사  >  php教程  >  PHP实现文件下载功能

PHP实现文件下载功能

WBOY
WBOY원래의
2016-06-06 20:12:091461검색

前面写过一篇PHP实现文件上传功能,后来还有从远程服务器通过http下载文件的需求,又写了个提供文件下载的脚本。 PS. 偶尔用用PHP写点这种很小很小的Web程序,还是蛮简单方便的。 PHP实现文件下载功能的代码如下 ?php$base_dir = "/usr/share/nginx/html/";$

前面写过一篇“PHP实现文件上传功能”,后来还有从远程服务器通过http下载文件的需求,又写了个提供文件下载的脚本。
PS. 偶尔用用PHP写点这种很小很小的Web程序,还是蛮简单方便的。
PHP实现文件下载功能的代码如下

<?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

(我是路过写几个PHP程序,就没有深入研究了)。

Original article: PHP实现文件下载功能

©2014 笑遍世界. All Rights Reserved.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.