Download file from non public html folder with php

后端 未结 2 930
失恋的感觉
失恋的感觉 2021-01-20 03:24

I have a number of files that are stored on the server, but not in the public_html directory. The idea is that users who are logged in can download the files, using $_SESSIO

2条回答
  •  遥遥无期
    2021-01-20 03:49

    You can use readfile to get output as the file in question. EG:

    $file = '/absolute/path/to/file.ext';
    
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
    }
    

提交回复
热议问题