本文章提供的三款文件下载代码有二款是支持本地服务器文件下载的,另一款支持下载远程服务器上的文件下载到本地,代码如下:
- function download($file_dir,$file_name)
-
-
-
- {
- $file_dir = chop($file_dir);
-
- if($file_dir != '')
- {
- $file_path = $file_dir;
- if(substr($file_dir,strlen($file_dir)-1,strlen($file_dir)) != '/')
- $file_path .= '/';
- $file_path .= $file_name;
- }
- else
- $file_path = $file_name;
-
-
- if(!file_exists($file_path))
- {
- echo '对不起,你要下载的文件不存在。';
- return false;
- }
-
- $file_size = filesize($file_path);
-
- header("content-type: application/octet-stream");
- header("accept-ranges: bytes");
- header("accept-length: $file_size");
- header("content-disposition: attachment; filename=".$file_name);
-
- $fp = fopen($file_path,"r");
- $buffer_size = 1024;
- $cur_pos = 0;
-
- while(!feof($fp)&&$file_size-$cur_pos>$buffer_size)
- {
- $buffer = fread($fp,$buffer_size);
- echo $buffer;
- $cur_pos += $buffer_size;
- }
-
- $buffer = fread($fp,$file_size-$cur_pos);
- echo $buffer;
- fclose($fp);
- return true;
-
- }
- <?php
-
- $file_name = "info_check.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 = "info_check.exe";
- $file_dir = " http://www.phpfensi.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);
- }
-
- ?>
|