PHP ?? ? ???? ?? ?? ?? ?? ??(2)
???? ??? ???? ?? download.php ??? ????, ?? ??? ???? ??? ??????, ????? ?? ????? ?????.
URL? ?? ????? ????, ?? ???? ???? ?? ??? ????, ????? ??? ????? ?????. ???? ?? ?? ???? ???? ?? +1, ??????? ?? ???? ??? ???????. +1, header()? ???? ???? ??? ?????. ??? ???? ??? "??? ???? ????!"? ?????.
header() ??? ???? ??? ??? ????? ? ???, ???? ? ??? ????? ?? ??? ??? ? ??? ?? ??? ??? ????.
????? ??? ???? ??? ??? ??? ??? ???? ?? ????? ??? ????? ?????. ? ??? ?? ? ??? ?? ??? ????? ???? ?? ???? ????. . ??? ????? ??? ?? header("Content-Disposition: attachment; filename=" .$filename)? ???? ?? ??? ?? ??? ? ?? ?? ???? ??? ? ????.
<?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']);//中文名稱注意轉(zhuǎn)換編碼 $savename = $row['savename']; //實(shí)際在服務(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 '文件不存在!'; } ?>
??:
iconv ?? ?????? ??? ?? ?? ?? ??? ??? ? ??? PHP ?????? ???? ? ? ?? ?? ????????.
file_exists() ??? ???? ????? ????? ?????. ??? ???? ????? ???? true? ????, ??? ??? false? ?????.
fopen() ??? ???? URL? ???. ??? ???? ? ??? FALSE? ?????. "r"? ?? ?? ??? ??? ?? ???? ?? ??? ?????.
feof() ??? ?? ?(eof)? ????? ??? ?????.
fread() ??? ??? ????(???? ??? ???).
fclose() ??? ?? ?? ??? ????.