PHP output file on disk to browser

前端 未结 6 2071
轻奢々
轻奢々 2020-11-27 19:18

I want to serve an existing file to the browser in PHP. I\'ve seen examples about image/jpeg but that function seems to save a file to disk and you have to create a right si

6条回答
  •  北海茫月
    2020-11-27 20:16

    I use this

      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;
    
        } 
    

提交回复
热议问题