重定向PHP自动下载.dat文件

时间:2016-02-18 11:02:47

标签: php redirect download

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

档案目录= http://domain.com/storage/file.dat 虽然thankyou.php在根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(): /home/user/public_html/dir/success.php 160 http://domain.com/path/to/file/filename.dat的统计信息失败>

2 个答案:

答案 0 :(得分:0)

在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

希望它会对你有所帮助:)。

答案 1 :(得分:0)

您只需创建一个脚本即可将标头设置为文件。例。

<?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。让我知道它是否有效;)