PHP:在readfile之后使用echo

问题描述 投票:1回答:1

我正在开发一个Web应用程序。

如果已正确生成zip,则用户可以使用readfile()下载特定的zip文件。

但是因为我已经集成了readfile()行,所以我无法在此行之前或之后回显任何内容。以下是导致问题的代码示例:

 if($zip->status == 0){

    $zip->close();

    $file_url = './zip/'.$userDir.'.zip';
    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: Binary");
    header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
    readfile($file_url);

    echo'
    <div class="alert alert-primary" role="alert">
    Success
    </div>';

  }

调用条件中的所有内容,除了最后的回声线。当我删除readfile($ file_url)行时,将调用echo。

我还尝试在线前移动回波线

$file_url = './zip/'.$userDir.'.zip';

它也不起作用。我错过了什么吗?

php echo readfile
1个回答
1
投票

如果在“发送文件”之后回显给用户,则会损坏文件。

您必须将PHP代码视为下载链接,用户单击它并获取文件。由于您有此标题,因此发送回送也不会进入用户屏幕

header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
© www.soinside.com 2019 - 2024. All rights reserved.