header() 下载后没有任何内容的文件
我正在尝试创建一个下载,以便用户单击“向下”,它将某个文件从他们的帐户下载到他们的计算机,我目前正在使用这个:
header('Content-Disposition: attachment; filename=' . basename("users/$username/$file_folder/$file_name"));
header("Content-Type:" .$file_type);
header("Content-Description: File Transfer");
header("Cache-control: private");
header("Connection: close");
header("Content-Length: ".$file_size);
问题是,文件正在下载,但它只是空的,文件中没有内容
之前的代码只是一个 if() 和一个带有数据库记录的 while 循环。 提前致谢。
I'm trying to create a download so that a user clicks on "down" it downloads a certain file from their account to their computer, I'm currently using this:
header('Content-Disposition: attachment; filename=' . basename("users/$username/$file_folder/$file_name"));
header("Content-Type:" .$file_type);
header("Content-Description: File Transfer");
header("Cache-control: private");
header("Connection: close");
header("Content-Length: ".$file_size);
The problem is, the file is downloading, but it's just empty, there is no content in the file
The code before this is just an if() and a while loop with database records.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
:(除非文件非常大,在这种情况下您会将其分块)
或者您可以使用要放入文件中的数据填充变量并简单地回显它,如下所示:
您缺少如下所示的内容 会在你的标题之后放在那里。希望这有帮助。
You are missing something like below: (unless the file is very large, in which case you would chunk it out)
Alternatively you could populate a variable with the data you want put out into the file and simple echo it out like so:
The content would be put out there AFTER your headers. Hope this helps.
该文件是空的,因为您从未输出该文件。这些
header
调用只是标头,您仍然需要一个正文才能正确下载文件。您可以使用file_get_contents
回显文件内容。The file is empty, because you never output the file. These
header
calls are just the header, you still need a body for a file to be correctly downloaded. You can usefile_get_contents
to echo the file contents.发送标头后,您需要实际推出文件内容...
请参阅此
http://php.net/manual/en/function.file-put -contents.php
after you send the headers you need to actually push out the file content...
see this
http://php.net/manual/en/function.file-put-contents.php