首页 > 代码库 > PHP文件下载
PHP文件下载
<?php header("Content-Type:text/html; charset=utf-8"); function download($file_path){ $file_path=iconv("utf-8","gb2312",$file_path); if(!is_file($file_path)){ die ("<b>file not found!</b>"); } if(!file_exists($file_path)){ die("<b>file not found!</b>"); } $dir_name = dirname($file_path); $file_name = basename($file_path); $len = filesize($file_path); $file_extension = strtolower(substr(strrchr($file_name,"."),1));//文件后缀 //根据文件后缀判断content-type switch ($file_extension) { case "pdf" : $ctype = "application/pdf"; break; case "exe" : $ctype = "application/octet-stream"; break; case "zip" : $ctype = "application/zip"; break; case "doc" : $ctype = "application/msword"; break; case "xls" : $ctype = "application/vnd.ms-excel"; break; case "ppt" : $ctype = "application/vnd.ms-powerpoint"; break; case "gif" : $ctype = "image/gif"; break; case "png" : $ctype = "image/png"; break; case "jpeg" : case "jpg" : $ctype = "image/jpg"; break; case "mp3" : $ctype = "audio/mpeg"; break; case "wav" : $ctype = "audio/x-wav"; break; case "mpeg" : case "mpg" : case "mpe" : $ctype = "video/mpeg"; break; case "mov" : $ctype = "video/quicktime"; break; case "avi" : $ctype = "video/x-msvideo"; break; // The following are for extensions that shouldn‘t be downloaded // (sensitive stuff, like php files) case "php" : case "htm" : case "html" : case "txt" : die ( "<b>Cannot download " . $file_extension . " files!</b>" ); break; default : $ctype = "application/force-download"; } //缓存控制 header("Cache-Control: public" ); header("Cache-Control: must-revalidate, post-check=0, pre-check=0" ); header("Pragma: no-cache" ); header("Expires: 0" ); header("Content-Description: File Transfer" ); //设置下载文件类型以及文件名 header("Content-type: ".$ctype); header("Accept-Ranges: bytes"); header("Content-Disposition: attachment; filename=".$file_name); header("Content-Transfer-Encoding: binary" ); header("Content-Length: ".$len ); //读取文件并写给客户端 $resource= fopen($file_path,"rb"); $block_size= 1024; $count = 0; while(!feof($resource) && $count<$len){ echo fread($resource,$block_size); $count+= $block_size; } fclose($resource); } //=============================begin process================================== $dir="D:\\"; if(!is_dir($dir)){ die("dir not right!"); } $dir= rtrim(str_replace("\\","/",$dir),"/")."/"; //接受请求参数 if(!empty($_GET) && isset($_GET["file"])){ $file = $_GET["file"]; } if(isset($file)){//如果有请求参数,就下载 download($dir.$file); }else{//打印出对应文件夹下面的文件(不递归,不包含文件夹) $dir=iconv("utf-8","gb2312",$dir); $files = array(); if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if(is_file($dir.$file)){ array_push($files,$file); } } closedir($dh); } } if(empty($files)){ echo "<b>no file in directory $dir</b>"; }else{ foreach($files as $one){ $one = iconv("gb2312","utf-8",$one); echo "<p>$one <a href=http://www.mamicode.com/‘download.php?file=$one‘>点击下载</a></p>"; } } }?>
PHP文件下载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。