调用文件下载标头后的 PHP 调用函数
我有一个创建文件供下载的函数,如下所示:
header("Content-type: application/octet-stream");
header("Content-disposition: filename=file.csv");
print 'hello hello';
exit;
$this->nextFuntion();
我需要在弹出保存对话框后调用“nextFunction”,我在这里看到了各种方法,但它们都与 header('location:') 有关特征。基本上,调用函数的页面包含文件内部的数据。 “nextFunction”将删除页面中的数据并返回消息等。
但我能做的最好的就是让下载工作并且没有任何功能。
I have a function creating a file for download like so:
header("Content-type: application/octet-stream");
header("Content-disposition: filename=file.csv");
print 'hello hello';
exit;
$this->nextFuntion();
I need the "nextFunction" to be called after the save dialogue pops up, I've seen various methods around here but they all have to do with header('location:') feature. Basicly the page where the function is called contains the data that goes inside the file. the "nextFunction" will erase the data form the page and return a message etc.
But the best I can do is get the download to work and no function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其分成两个脚本。第一个将调用该函数,然后重定向到第二个(通过 META 刷新等),第二个将负责发送要下载的文件。
Split this into two scripts. The first will call the function, and then redirect to the second (via a META refresh or the like) which will be responsible for sending the file to be downloaded.
您确定要在下载文件后立即删除该文件吗?如果用户想再次下载它但它不可用,则此功能可能会惹恼他们。我建议设置一个 cron 作业来删除所有早于特定时间的文件。
Are you sure you always want to erase the file immediately after it's downloaded? If the user wants to download it again and it's unavailable, this functionality could annoy them. I'd recommend setting up a cron job to delete all files that are older than a certain time.
我认为你的问题更多地与 nextFunction() 有关,因为 PHP 应该允许你即使在设置标头之后也可以调用函数。
尝试使用仅记录到文件的测试函数或我个人最喜欢的 exec("echo 'HAY HAY' | wall ");
I think your issue is more to do with nextFunction() as PHP should allow you to call functions even after headers are set.
Try it with a test function that just logs to a file or my personal favorite exec("echo 'HAY HAY' | wall ");