在php中动态创建并下载文件
我正在动态创建一个 Excel 文件(数据来自数据库)。调用 URL 时,它工作正常,显示下载对话框。但每当我使用 CURl 函数调用此 URL 时,我想动态地将此文件保存在文件夹中(即在我的服务器硬盘中)。 我如何设置标题,以便创建 Excel 文件并将其保存在文件夹中。 我正在使用的标头代码,
<?php
$name = "myFile";
$file_ending = "xls";
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename={$name}.{$file_ending}");
header("Pragma: no-cache");
header("Expires: 0");
echo "the data...";
?>
或者是否有其他方法可以这样做。 注意-我希望这样,因为该函数将由 Web 服务器调度程序调用。
I am creating an Excel file dynamically(data coming from database). on calling the URL it works fine it shows the download dialog box. but i want to save this file in a folder(i.e in my servers harddisk) dynamically whenever i call this URL using CURl function.
How can i set the headers in such a way that it will create the Excel file and save it in a folder.
The header code that i am using,
<?php
$name = "myFile";
$file_ending = "xls";
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename={$name}.{$file_ending}");
header("Pragma: no-cache");
header("Expires: 0");
echo "the data...";
?>
or is there any other method to do so.
NOTE-I want it this way because, this function will be called by the web servers scheduler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法通过设置标头将某些内容保存在某个特定路径中。 HTTP 标头仅帮助通过 HTTP 网络连接传输数据,而它对文件系统一无所知。
也许您只想将数据写入文件?
You cannot save something in some specific path by setting headers. HTTP headers specifically only help transport data across an HTTP network connection, which knows nothing of file systems.
Perhaps you just want to write the data into a file instead?
创建一个空目录,假设
将其称为“downloads”,
在“downloads”目录中创建一个 .htaccess 文件,其中包含以下内容
在“downloads”目录中创建一个名为 dummy.php 的文件
通过将目录保持为空,每次找不到该文件,它调用 dummy.php
您希望用户下载的任何文件,将其指向 /downloads/any_madeup_filename
Its easy
Create an empty directory, lets say call it "downloads"
create an .htaccess file in the "downloads" directory with the following
Create a file in the "downloads" directory called dummy.php
By keeping the directory empty, every time it can't find the file, it calls dummy.php
Any file you want the user to download, point them to /downloads/any_madeup_filename