How to return a file in PHP

前端 未结 4 1161
别跟我提以往
别跟我提以往 2020-11-28 07:41

I have a file

/file.zip

A user comes to

/download.php

I want the user\'s browser to start downloading the

4条回答
  •  天涯浪人
    2020-11-28 08:23

    I think you want this:

            $attachment_location = $_SERVER["DOCUMENT_ROOT"] . "/file.zip";
            if (file_exists($attachment_location)) {
    
                header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
                header("Cache-Control: public"); // needed for internet explorer
                header("Content-Type: application/zip");
                header("Content-Transfer-Encoding: Binary");
                header("Content-Length:".filesize($attachment_location));
                header("Content-Disposition: attachment; filename=file.zip");
                readfile($attachment_location);
                die();        
            } else {
                die("Error: File not found.");
            } 
    

提交回复
热议问题