Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向用户发送文件';php中的浏览器_Php - Fatal编程技术网

向用户发送文件';php中的浏览器

向用户发送文件';php中的浏览器,php,Php,我不是很擅长php,所以如果这个问题对你来说很愚蠢,请原谅我。:) 我制作了一个简单的脚本,它改进了文本字幕文件(.srt)并将其发送回用户(作为newfile.srt)。而且效果很好。但有一件事我不明白。将文件发送到用户浏览器后: $tmpName = tempnam(sys_get_temp_dir(), 'newfile'); $file = fopen($tmpName, 'w') or die("Can't open file!"); $output = implode("\r\n",

我不是很擅长php,所以如果这个问题对你来说很愚蠢,请原谅我。:)

我制作了一个简单的脚本,它改进了文本字幕文件(.srt)并将其发送回用户(作为newfile.srt)。而且效果很好。但有一件事我不明白。将文件发送到用户浏览器后:

$tmpName = tempnam(sys_get_temp_dir(), 'newfile');
$file = fopen($tmpName, 'w') or die("Can't open file!");
$output = implode("\r\n", $com); // $com is an array with lines of the new file
fwrite($file, $output);
header('Content-Description: File Transfer');
header('Content-Type: text/srt');
header('Content-Disposition: attachment; filename=newfile.srt');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($tmpName));
ob_clean();
flush();
readfile($tmpName);
unlink($tmpName);
我想将用户重定向到另一个页面。就像这样:

header('Location: http://myserver.com/newpage.php');

但它似乎不起作用。即使我试着在上面写的几行字后回音,它似乎也不起作用——什么也没发生。你能告诉我为什么吗?我该怎么办?

您不能在同一个响应中发送两组标题

在您的案例中,您可以使用一些JavaScript来处理两个请求:

  • 在新选项卡中打开下载脚本。下载开始后,此选项卡将立即关闭
  • 将当前页面重定向到newpage.php
  • 您可以通过在表单上简单地设置target=“\u blank”来处理新选项卡,如果脚本当前是这样工作的。然后将一个函数绑定到onsubmit事件,以便随后重定向当前页面

    无论哪种方式,您都无法仅使用PHP实现这一点