PHP标头读取文件和重定向

时间:2015-08-29 06:44:19

标签: php header

我正在尝试在下载文件后重定向用户。我设法让他们下载文件,但我不能将它们重定向到网址。请求帮助

<?php
//Search for file using GET
if(isset ($_GET['file']) && ($_GET['url'])){

    $file = $_GET['file'];
    $link = $_GET['url'];

    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));
        readfile($file);
        exit;

    } else {

        echo "Why are you here?";
    }

//file downloaded now go to link
header('Location: $link');

}

?>

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:0)

不,你做的和想要的都不可能。

  • 如果存在GET参数 file url 并且物理文件不存在,则会执行重定向。
  • 如果存在GET参数 file url 并且物理文件存在,则在下载后退出(脚本在进一步执行时停止)。

为什么不从html代码重定向到您的下载脚本。许多下载站点使用。

as pseudo,redirect.html:

html
   head
      redirect to your forced download file FOO after X seconds with e.g. meta tag
   /head
   body
      Download of file FOO starts in X seconds
   /body
/html

在上面的情况下,你不需要php重定向,只需要强制下载。将重定向脚本设置为与下载脚本不在同一文件中!

答案 1 :(得分:-1)

你得错了报价:

header("Location: $link"); // note the "

或者连接:

header('Location: '.$link);

顺便说一下,你正在做你想做的事情的方式基本上是一个让HTTP 80访问这个脚本的人下载你服务器上的任何文件的好方法。如果$file = '/etc/passwd';怎么办?

答案 2 :(得分:-1)

删除退出;从真正的部分,并尝试把它改为

header('Location: $link');