作业帮 > PHP > 教育资讯

PHP教程:PHP下载文件的代码

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/10 23:17:38 PHP
PHP教程:PHP下载文件的代码
PHP教程:PHP下载文件的代码PHP
【51Test.NET-PHP教程:PHP下载文件的代码】:
2header("Content-Type: application/force-download");
3header("Content-Disposition: attachment; filename=ins.jpg");
4readfile("imgs/test_Zoom.jpg");
5?>

第一行代码是强制下载;

第二行代码是给下载的内容指定一个名字;

第三行代码是把下载的内容读进文件中。


function downloadfile(){                       //下载FTP中的一个文件     (已测试成功) 
        $file_name = "aa.pdf";
        $file_dir ="./";
        if (!file_exists($file_dir."/".$file_name)){ //检查文件是否存在
         return false;
         exit;
        }else{
         $file = fopen($file_dir."/".$file_name,"r"); // 打开文件
         // 输入文件标签
         header('Content-Encoding: none');
         header("Content-type: application/octet-stream");
         header("Accept-Ranges: bytes");
         header("Accept-Length: ".filesize($file_dir."/".$file_name));
         header( 'Content-Transfer-Encoding: binary' );
         header("Content-Disposition: attachment; filename=" .$file_name); //以真实文件名提供给浏览器下载 
         header('Pragma: no-cache');
         header('Expires: 0');
         //输出文件内容
         echo fread($file,filesize($file_dir."/".$file_name));
         fclose($file);
         exit;
        }
      } 
      downloadfile();
?>
PHP