This article is about the PHP download file code, has a certain reference value, now share to everyone, the need for friends can refer to the
Public function down () {header (' Content-type:application:json;charset=utf8 '); Header (' access-control-allow-origin:https://coucouchina.com '); Header (' Access-control-allow-methods:post '); Header (' Access-control-allow-headers:x-requested-with,content-type '); Header ("Content-type:text/html;charset=utf-8");//$file _name= "cookie.jpg"; $file _name = "apple-app-site-association";//To solve the problem of Chinese can not be displayed $file _name = Iconv ("Utf-8", "gb2312", $file _name); $file _sub_path = $_server[' Document_root '). "/"; $file _path = $file _sub_path. $file _name;//first to determine if the given file exists or not (!file_exists ($file _path)) {echo "does not have the file file"; Return } $fp = fopen ($file _path, "R"); $file _size = filesize ($file _path);//Headers ("Content-type:application/octet-stream") to be used for downloading files; Header ("Accept-ranges:bytes"); Header ("Accept-length:".) $file _size); Header ("content-disposition:attachment; Filename= ". $file _name); $buffer = 1024; $file _count = 0;//returns data to the browser while (!feof ($fp) && $file _count < $file _size) {$file _con = Fread ($fp, $buffer); $file _count + = $buffer; echo $file _con; } fclose ($FP); }