【问题标题】:Redirect PHP to download .dat file automatically重定向 PHP 以自动下载 .dat 文件
【发布时间】:2016-02-18 11:02:46
【问题描述】:

如何自动重定向thankyou.php页面以下载位于不同目录的“.DAT”文件。

文件目录 = http://domain.com/storage/file.dat 虽然thankyou.php 可在root http://domain.com/thankyou.php

感谢大家回答我的问题。

在我非常感谢@bruno-fidelis 之后,我收到了有关下载文件的以下错误消息。我的意思是当文件被下载并打开时,我会收到此错误消息

<html>
<head>
<title>Thank You</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<br />
<b>Warning</b>:  readfile(http://domain.com/path/to/file/filename.dat): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
in <b>/home/user/public_html/dir/success.php</b> on line <b>159</b><br />


警告:filesize(): stat failed for http://domain.com/path/to/file/filename.dat in /home/user/public_html/dir/success.php 160

【问题讨论】:

标签: php redirect download


【解决方案1】:

在您的 PHP 文件中设置标题以自动下载文件。

header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($File));
header("Connection: close");

要从服务器下载更大的文件,请更改 php.ini 文件中的以下设置:

Upload_max_filesize  - 1500 M
Max_input_time  - 1000
Memory_limit    - 640M
Max_execution_time -  1800
Post_max_size - 2000 M

也请参考link

希望对你有帮助:)

【讨论】:

  • 感谢您的分享,但它没有下载完整大小的文件:)
  • 谢谢拉维,我想我的信息设置得很好,但我会仔细检查
【解决方案2】:

您可以简单地编写一个脚本来设置文件的标题。例子。

<?php

$file = file_get_contents('path/to/file.dat');
$size = filesize('path/to/file.dat');
$quotedFileName = '"'. basename('path/to/file.dat') .'"';

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $quotedFileName);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
flush();
echo $file;
exit();

我建议您阅读有关http headers 的信息。让我知道它是否有效;)

【讨论】:

  • 感谢分享,确实有帮助,但仍然面临大小问题。
  • 什么样的尺寸问题?如果文件太大,您必须配置 php.ini 文件以接受更大的帖子大小check this link
  • 文件大小在1MB到4MB之间
  • @WiTonNope 我在标题后添加了 flush(),试一试。只是好奇,为什么你说这是一个尺寸问题?
  • 抱歉回复晚了,感谢更新代码。不幸的是它也没有工作。我说这是大小问题,因为文件是 1.74MB,下载时我只得到 776B,甚至还不到实际大小的一半。
猜你喜欢
  • 1970-01-01
  • 2010-10-23
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
  • 2011-08-30
  • 1970-01-01
相关资源
最近更新 更多