代碼如下 |
複製代碼 |
function download($file_dir,$file_name) //參數說明: //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;}
|
?>
<?
// 如果檔案路徑是http和ftp,下載代碼如下:
代碼如下 |
複製代碼 |
$file_name = "info_check.exe"; $file_dir = " http://www.111cn.net/"; $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); } ?> |