[PHP]使用header()下载代码无效

时间:2015-09-12 03:10:16

标签: php download

我正在开发文件共享服务,我尝试制作下载代码,但只显示白页!

以下是down.php

的代码
<?php
$var = $_GET["fid"];

        $fr = 'realname.txt'; //fake file name.
$filepath = '/home/user32222/public_html/parafile/'.$var.'/filestorage/downloadthisfile.txt' //In this case,  file extension is txt but I want to download file with any extension even file extension is nothing
//I tried these codes but it not worked!!
                    /*header('Pragma: public');     // required 
                    header('Expires: 0');       
                    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                    header('Content-Type: application/octet-stream');
                    header("Content-Disposition: attachment; filename=\"$fr\"");
                    header('Content-Length: ' . filesize($filepath));
                    readfile($filepath);*/


                    $size   = filesize($filepath);
                    header('Content-Description: File Transfer');
                    header('Content-Type: application/octet-stream');
                    header('Content-Disposition: attachment; filename=' . $fr); //Download Client Program think file's name is realname.txt ($fr)

                    header('Content-Transfer-Encoding: binary');
                    header('Connection: Keep-Alive');
                    header('Expires: 0');
                    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                    header('Pragma: public');
                    header('Content-Length: ' . $size);


?>

1 个答案:

答案 0 :(得分:0)

由于您没有读取要下载的文件,因此返回的响应为空。这可以使用mock_data = mock.MagicMock() mock_data.status_code = 200 # TODO (Low Kian Seong): Test raise_for_status mock_head.return_value = mock_data ... mock_data.raise_for_status.assert_called_once_with() 函数

完成

试试这段代码:

readfile()