How to force file download with PHP

后端 未结 11 1798
被撕碎了的回忆
被撕碎了的回忆 2020-11-21 23:13

I want to require a file to be downloaded upon the user visiting a web page with PHP. I think it has something to do with file_get_contents, but am not sure how

11条回答
  •  清歌不尽
    2020-11-21 23:26

    The following code is a correct way of implementing a download service in php as explained in the following tutorial

    header('Content-Type: application/zip');
    header("Content-Disposition: attachment; filename=\"$file_name\"");
    set_time_limit(0);
    $file = @fopen($filePath, "rb");
    while(!feof($file)) {
        print(@fread($file, 1024*8));
        ob_flush();
        flush();
    }
    

提交回复
热议问题