PHP output file on disk to browser

前端 未结 6 2075
轻奢々
轻奢々 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条回答
  •  Happy的楠姐
    2020-11-27 20:22

    Following will initiate XML file output

    $fp = fopen($file_name, 'rb');
    
    // Set the header
    header("Content-Type: text/xml");
    header("Content-Length: " . filesize($file_name));
    header('Content-Disposition: attachment; filename="'.$file_name.'"');
    
    fpassthru($fp);
    exit;
    

    The 'Content-Disposition: attachment' is pretty common and is used by sites like Facebook to set the right header

提交回复
热议问题