从PHP变量下载/执行数据


Download/execute data from PHP variable

我试图在客户端下载PHP缓冲区输出。我对将输出内容写入服务器上的文件然后允许下载不感兴趣。我通过在浏览器上打印变量输出来检查变量输出。由于缓冲区应该在客户端作为文件下载,我应该在哪里指定文件名?

<?php
  $file=$_POST['file'];
  header('Content-Description: File Transfer');
  header('Content-Type: application/x-java-jnlp-file');  
  ob_clean();
  flush();
  readfile($file);
 ?>

~

你需要"Content-Disposition"标题:

header( 'Content-Disposition: attachment; filename="downloaded.pdf"' );

完整示例:http://www.php.net/manual/en/function.header.php#example-4581

直接使用这个方法

<?php
$file = 'monkey.gif';
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

需要更多帮助http://www.php.net/manual/en/function.readfile.php