<?PHPinclude("DBDA.class.php");$db=NewDbda ();$bs=$_server["Query_string"];//Directget the parameters passed by the submission interface, and note that you should use the <a> label, <form> form, or window.location.href= "chuli.php?id=" +id+ "" to pass the value, not the AJSX. $BSS=substr($bs, 3);//Intercept = The following value, according to the value passed to intercept, depending on the situation. $sql= "Select video from Shangpin where id= ' {$BSS}‘";//gets the path to the video file and writes the SQL statement as appropriate. $str=$db->strquery ($sql);$WJM=substr($str, 13);//intercept the file name and intercept it according to actual situation. $lj=substr($str, 0,13);//intercepts the folder path where the file is located and intercepts it according to actual situation. $file _name=$WJM;//file name//To solve problems that cannot be displayed in Chinese$file _name=Iconv("Utf-8", "gb2312",$file _name); $file _sub_path=$_server[' Document_root '].$lj;//gets the document root directory where the current run script resides$file _path=$file _sub_path.$file _name;//the path to which the complete file is to be spelled//$file = include path
Var_dump (file_exists ($file _path))//Can first determine whether the file exists, note that the file path can not exist in Chinese characters, all characters are English or letter. The following code does not need to be changed.
if(file_exists($file _path))//determine if a file exists{ Header(' Content-description:file Transfer ');//The header function is submitted to the table header is some of the download specifications Header(' Content-type:application/octet-stream '); Header(' Content-disposition:attachment; Filename= '.basename($file _path)); Header(' Content-transfer-encoding:binary '); Header(' expires:0 '); Header(' Cache-control:must-revalidate, post-check=0, pre-check=0 '); Header(' Pragma:public '); Header(' Content-length: '.filesize($file _path)); Ob_clean();//Ob_clean This function is used to discard the contents of the output buffer, if your site has a lot of generated files, you want to access the correct, you should always clear the buffer Flush();//the difference between ob_flush () and flush (). The former is the release of data from the buffer of PHP, the latter is not buffered or released data sent to the browser. So when the buffers are present, we have to use both ob_flush () and flush (). $file=fopen($file _path, "R");//opens the specified file, R represents read-only, and returns false if not found while(!feof($file))//determine if there is { //send The current file part to the browser Print fread($file,round(3000 * 1024));//Top Download speed 3MB//flush the content to the browser Flush();//Pass to Browser//sleep one second Sleep(1);//wait 1 seconds } fclose($file);//Close File /*ReadFile ($file _path); You can also use this method to send out the table header directly output, but there is no speed limit exit;*/}
Note the points:
1. The transmitted value is intercepted according to the actual situation;
2. filename and file path are also intercepted according to the actual situation;
3. File path can not exist Chinese characters, the existence of Chinese characters to determine whether the existence of the file will always return to flase;
4.if inside the code does not need to modify;
5. $bs = $_server["query_string"); Can directly accept the value passed;
PHP to do file speed limit download