将数据从 Drupal 保存到本地系统文件

发布于 2024-08-21 14:39:04 字数 281 浏览 4 评论 0原文

我创建了一个显示一些报告数据的 drupal 模块。我想包含一个选项,使查看页面的用户能够将数据保存为本地系统上的 txt 或 csv 文件,但我遇到了以下问题:

  1. 如何指定要保存到的本地目录。
  2. 如何实际将数据保存到目录中。

我已经阅读了表单的 drupal api 文档。有一种“文件”输入类型,允许用户浏览其系统并选择从中上传的位置。我还查看了 file_save_data() 函数,但似乎数据仅保存在 drupal 站点目录中。

任何帮助将不胜感激。

I have created a drupal module which displays some reporting data. I would like to include an option that enables a user viewing the page to save data as a txt or csv file on their local system, but I am running into the following problems:

  1. How to specify local directory to save to.
  2. How to actually save the data to the directory.

I've read through the drupal api documentation for forms. There is a 'file' input type that allows a user to browse their system and select a location to upload from. I've also looked at the file_save_data() function, but it seems that data is only saved within the drupal site directory.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街女流氓 2024-08-28 14:39:04

如何指定保存到的本地目录。

你不能。用户的浏览器会将您的文件保存到浏览器的默认下载目录,或者提示用户选择一个位置。让网站选择文件的保存位置将是一个主要的安全漏洞——他们可以创建病毒的桌面快捷方式、添加启动项等。

How to specify local directory to save to.

You can't. The user's browser will save your files to either the browser's default downloads directory, or it'll prompt the user to pick a location. Letting a website pick where a file gets saved would be a major security hole - they could create desktop shortcuts to viruses, add startup items, etc.

A君 2024-08-28 14:39:04

这对我有用:

$file = file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME); 
drupal_set_header($header = "Content-type: text");
drupal_set_header($header = "Content-Disposition: attachment; filename=$file.xls"); 
readfile($file);  
die(); 

die() 需要进入那里,否则页面 html 将被附加到文件中。

This worked for me:

$file = file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME); 
drupal_set_header($header = "Content-type: text");
drupal_set_header($header = "Content-Disposition: attachment; filename=$file.xls"); 
readfile($file);  
die(); 

The die() needs to go in there, or else the page html will be appended to the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文