PHP development...LOGIN

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('参数错误!');
$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']);//中文名称注意转换编码
$savename = $row['savename']; //实际在服务器上的保存名称
$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);  //打开文件最大字节数为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.



Next Section
<?php require('conn.php'); $id = (int)$_GET['id']; if(!isset($id) || $id==0) die('参数错误!'); $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']);//中文名称注意转换编码 $savename = $row['savename']; //实际在服务器上的保存名称 $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); //打开文件最大字节数为50000 } fclose($file); exit; }else{ echo '文件不存在!'; } ?>
submitReset Code
ChapterCourseware