亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP開(kāi)發(fā)實(shí)作下載次數(shù)統(tǒng)計(jì)功能模組(二)

建立download.php文件,用來(lái)回應(yīng)下載動(dòng)作,更新對(duì)應(yīng)文件的下載次數(shù),並且透過(guò)瀏覽器完成下載。

根據(jù)url傳參,查詢得到對(duì)應(yīng)的數(shù)據(jù),檢測(cè)要下載的檔案是否存在,如果存在,則更新對(duì)應(yīng)數(shù)據(jù)的下載次數(shù)+1,資料庫(kù)中的檔案下載次數(shù)+1,並且使用header ()實(shí)現(xiàn)下載功能。如果文件不存在,則輸出「文件不存在!」。

值得一提的是,使用header()函數(shù),強(qiáng)制下載文件,並且可以設(shè)定下載後儲(chǔ)存到本地的文件名稱(chēng)。

一般情況下,我們透過(guò)後臺(tái)上傳程式會(huì)將上傳的文件重新命名後保存到伺服器上,常見(jiàn)的有以日期時(shí)間命名的文件,這樣的好處之一就是避免了文件名重複和中文名稱(chēng)亂碼的情況。而我們下載到本機(jī)的檔案可以使用header("Content-Disposition: attachment; filename=" .$filename )將檔案名稱(chēng)設(shè)定為易於識(shí)別的檔案名稱(chēng)。

<?php
require('conn.php');
$id = (int)$_GET['id'];

if(!isset($id) || $id==0) die('參數(shù)錯(cuò)誤!');
$query = mysqli_query($link,"select * from downloads where id='$id'");
$row = mysqli_fetch_array($query);
if(!$row) exit;
$filename = iconv('UTF-8','GBK',$row['filename']);//中文名稱(chēng)注意轉(zhuǎn)換編碼
$savename = $row['savename']; //實(shí)際在服務(wù)器上的保存名稱(chēng)
$myfile = 'files/'.$savename;  //文件名稱(chēng)
if(file_exists($myfile)){
   mysqli_query($link,"update downloads set downloads=downloads+1 where id='$id'");
   $file = @fopen($myfile, "r"); 
   header("Content-type: application/octet-stream");
   header("Content-Disposition: attachment; filename=" .$filename );
   while (!feof($file)) {
      echo fread($file, 50000);  //打開(kāi)文件最大字節(jié)數(shù)為50000
   }
   fclose($file);
   exit;
}else{
   echo '文件不存在!';
}
?>

註解:

iconv函式庫(kù)能夠完成各種字元集間的轉(zhuǎn)換,是php程式設(shè)計(jì)中不可缺少的基礎(chǔ)函式庫(kù)。

file_exists() 函數(shù)檢查檔案或目錄是否存在。 如果指定的檔案或目錄存在則傳回 true,否則傳回 false。

fopen() 函數(shù)開(kāi)啟檔案或 URL。 如果開(kāi)啟失敗,本函數(shù)傳回 FALSE。 「r」只讀方式打開(kāi),將檔案指標(biāo)指向檔案頭。

feof() 函數(shù)偵測(cè)是否已到達(dá)檔案結(jié)尾 (eof)。

fread()?函數(shù)讀取檔案(可安全用於二進(jìn)位檔案)。

fclose()函數(shù)關(guān)閉一個(gè)開(kāi)啟檔案。



#
繼續(xù)學(xué)習(xí)
||
<?php require('conn.php'); $id = (int)$_GET['id']; if(!isset($id) || $id==0) die('參數(shù)錯(cuò)誤!'); $query = mysqli_query($link,"select * from downloads where id='$id'"); $row = mysqli_fetch_array($query); if(!$row) exit; $filename = iconv('UTF-8','GBK',$row['filename']);//中文名稱(chēng)注意轉(zhuǎn)換編碼 $savename = $row['savename']; //實(shí)際在服務(wù)器上的保存名稱(chēng) $myfile = 'files/'.$savename; //文件名稱(chēng) if(file_exists($myfile)){ mysqli_query($link,"update downloads set downloads=downloads+1 where id='$id'"); $file = @fopen($myfile, "r"); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=" .$filename ); while (!feof($file)) { echo fread($file, 50000); //打開(kāi)文件最大字節(jié)數(shù)為50000 } fclose($file); exit; }else{ echo '文件不存在!'; } ?>
提交重置程式碼