欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php下載文件源代碼(強(qiáng)制任意文件格式下載)

 更新時(shí)間:2014年05月09日 15:24:30   作者:  
有時(shí)候我們需要用php下載一些文件,一般就是為了隱藏文件的真實(shí)下載地址才需要這樣,否則這樣會(huì)增加服務(wù)器負(fù)擔(dān),不如直接提供軟件的地址

一個(gè)簡(jiǎn)單的php文件下載源代碼,雖不支持?jǐn)帱c(diǎn)續(xù)傳等,但是可以滿足一些常用的需求了。php下載文件其實(shí)用一個(gè)a標(biāo)簽就能實(shí)現(xiàn),比如 <a href="web/magento-1.8.1.0.zip">magento-1.8.1.0.zip</a> 。但是遇到一些瀏覽器能識(shí)別的格式,比如.txt,.html,.pdf等,再用<a href="web/abc.txt">abc.txt</a> 想必也知道會(huì)發(fā)生什么了。

復(fù)制代碼 代碼如下:

<?php
/**
 * 文件下載
 *
**/
header("Content-type:text/html;charset=utf-8");
download('web/magento-1.8.1.0.zip', 'magento下載'); 
function download($file, $down_name){
 $suffix = substr($file,strrpos($file,'.')); //獲取文件后綴
 $down_name = $down_name.$suffix; //新文件名,就是下載后的名字

 //判斷給定的文件存在與否
 if(!file_exists($file)){
  die("您要下載的文件已不存在,可能是被刪除");
 }
 $fp = fopen($file,"r");
 $file_size = filesize($file);
 //下載文件需要用到的頭
 header("Content-type: application/octet-stream");
 header("Accept-Ranges: bytes");
 header("Accept-Length:".$file_size);
 header("Content-Disposition: attachment; filename=".$down_name);
 $buffer = 1024;
 $file_count = 0;
 //向?yàn)g覽器返回?cái)?shù)據(jù)
 while(!feof($fp) && $file_count < $file_size){
  $file_con = fread($fp,$buffer);
  $file_count += $buffer;
  echo $file_con;
 }
 fclose($fp);
}
?>

PHP強(qiáng)制性文件下載的源代碼

為用戶提供強(qiáng)制性的文件下載功能。

復(fù)制代碼 代碼如下:

/********************
*@file - path to file
*/
function force_download($file)
{
if ((isset($file))&&(file_exists($file))) {
header("Content-length: ".filesize($file));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file . '"');
readfile("$file");
} else {
echo "No file selected";
}
}

你一定會(huì)笑我"下載文件"如此簡(jiǎn)單都值得說?當(dāng)然并不是想象那么簡(jiǎn)單。例如你希望客戶要填完一份表格,才可以下載某一文件,你第一個(gè)想法一定是用 "Redirect"的方法,先檢查表格是否已經(jīng)填寫完畢和完整,然后就將網(wǎng)址指到該文件,這樣客戶才能下載,但如果你想做一個(gè)關(guān)于"網(wǎng)上購(gòu)物"的電子商務(wù)網(wǎng)站,考慮安全問題,你不想用戶直接復(fù)制網(wǎng)址下載該文件,筆者建議你使用PHP直接讀取該實(shí)際文件然后下載的方法去做。程序如下:

復(fù)制代碼 代碼如下:

$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"); // 打開文件
// 輸入文件標(biāo)簽
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);
// 輸出文件內(nèi)容
echo fread($file,filesize($file_dir . $file_name));
fclose($file);
exit;
}

而如果文件路徑是"http" 或者 "ftp" 網(wǎng)址的話,則源代碼會(huì)有少許改變,程序如下:

復(fù)制代碼 代碼如下:

$file_name = "info_check.exe";
$file_dir = "http://www.dbjr.com.cn/";
$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);
}

這樣就可以用PHP直接輸出文件了。

但,一定要注意:Header信息相當(dāng)于先將文件信息高速瀏覽器,然后,再把瀏覽器上的信息下載到附件中。所以,如果在MVC模式的應(yīng)用程序中,view頁(yè)一定不要有任何內(nèi)容,否則,view頁(yè)的相關(guān)內(nèi)容會(huì)隨著文件的內(nèi)容一同被下載,導(dǎo)致下載后的文件不能使用。

下面是我的程序:

 

復(fù)制代碼 代碼如下:

   public function downloadAction()
    {
        if (isset($_GET['mriID']))
    {
    $this->view->mriID=(get_magic_quotes_gpc())?$_GET['mriID']:addslashes($_GET['mriID']);
    }
        if (isset($_GET['dicomID']))
    {
    $this->view->dicomID=(get_magic_quotes_gpc())?$_GET['dicomID']:addslashes($_GET['dicomID']);
    }
       if (isset($_GET['JPGID']))
    {
    $this->view->JPGID=(get_magic_quotes_gpc())?$_GET['JPGID']:addslashes($_GET['JPGID']);
    }
    $dicomfile=new dicomfile();
    $jpgfile=new jpgfile();
    $mri=new mri();
    if($this->view->dicomID)
    {
    $filename=$dicomfile->find($this->view->dicomID)->toArray();
    $filename=$filename[0]['filename'];
    }  
    else if($this->view->JPGID)
    {
      $filename=$jpgfile->find($this->view->JPGID)->toArray();
      $filename=$filename[0]['JPGname'];
    }
    $dir=$mri->find($this->view->mriID)->toArray();
    $dir=$dir[0]['dicom_path'];
    $file=$dir.'/'.$filename;
    if (!file_exists($file))
    {
    echo "the file does not exist!";
    exit();
    }
      $file_size=filesize($file);
           header("Content-type: application/octet-stream");
           header("Accept-Ranges: bytes");
           header("Accept-Length:". $file_size);
           header("Content-Disposition: attachment; filename=".$filename);
           $fp=fopen($file,"r");
      if (!$fp)
    echo "can't open file!";
       $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); 
    }

此時(shí),download.phtml頁(yè)面一定要是完全空白的。千萬不要有任何內(nèi)容(包括如下的固定信息:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>)否則,這些信息都將被下載到下載文件中,導(dǎo)致文件不能使用。

相關(guān)文章

最新評(píng)論