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

PHP development and implementation of download count statistics function module (2)

Create a download.php file to respond to the download action, update the number of downloads of the corresponding file, and complete the download through the browser.

Pass parameters according to the url, query to get the corresponding data, detect whether the file to be downloaded exists, if it exists, update the number of downloads of the corresponding data +1, the number of file downloads in the database +1, and use the header () implements the download function. If the file does not exist, "File does not exist!" is output.

It is worth mentioning that the header() function is used to force the file to be downloaded, and the file name can be set to be saved locally after downloading.

Under normal circumstances, we will rename the uploaded file and save it to the server through the background upload program. Common files are named after date and time. One of the benefits of this is to avoid duplication of file names and The Chinese name is garbled. For files we download locally, we can use header("Content-Disposition: attachment; filename=" .$filename) to set the file name to an easily identifiable file name.

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

if(!isset($id) || $id==0) die('參數(shù)錯誤!');
$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']);//中文名稱注意轉(zhuǎn)換編碼
$savename = $row['savename']; //實際在服務(wù)器上的保存名稱
$myfile = 'files/'.$savename;  //文件名稱
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);  //打開文件最大字節(jié)數(shù)為50000
   }
   fclose($file);
   exit;
}else{
   echo '文件不存在!';
}
?>

Note:

iconv function library can complete conversion between various character sets and is an indispensable basic function library in PHP programming.

file_exists() Function checks whether a file or directory exists. Returns true if the specified file or directory exists, otherwise returns false.

fopen() Function opens a file or URL. If the opening fails, this function returns FALSE. "r" opens in read-only mode and points the file pointer to the file header.

feof() Function detects whether the end of file (eof) has been reached.

fread() function reads a file (safe for binary files).

fclose()The function closes an open file.



Continuing Learning
||
<?php require('conn.php'); $id = (int)$_GET['id']; if(!isset($id) || $id==0) die('參數(shù)錯誤!'); $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']);//中文名稱注意轉(zhuǎn)換編碼 $savename = $row['savename']; //實際在服務(wù)器上的保存名稱 $myfile = 'files/'.$savename; //文件名稱 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); //打開文件最大字節(jié)數(shù)為50000 } fclose($file); exit; }else{ echo '文件不存在!'; } ?>
submitReset Code