使用php从服务器下载文件后立即重定向到任何url


Redirect to any url right after downloading file from server using php

Header("location:index.php"(;对我不起作用。

$file是文件的路径。

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));
    header("Content-Type: application/force-download");
    readfile($file);
    header("location:index.php");
    exit;
}

浏览器会调用服务器。服务器以标头("我将发送一个文件"(进行响应并发送内容。浏览器读取标题,而不是显示新页面,它为您提供了保存文件的功能。将忽略所有进一步的操作。

如果您同时想要文件下载和重定向,则必须将下载请求发送到另一个容器(新选项卡、新窗口、iframe(,并使用javascript将主窗口重定向到其他位置。