如何以编程方式将图像存储在 Drupal 站点上可公开访问的位置?
我正在编写一个 Drupal 模块,用户可以在其中发送内容(格式为 HTML)。内容几乎总是包含 标签。这些标签将引用用户的服务器。但是,我不想每次请求内容时都访问用户的服务器;我想在我的服务器上提供一个版本。
因此,我编写了一个函数来查找 HTML 中的 标签,然后使用 cURL 将它们保存在本地。一切都很好 - 除了我无法弄清楚本地文件系统中的何处存储图像,这要归功于 Drupal 疯狂的 URL 重写。理想情况下,我可以将它们放在可以通过网络访问的目录中,而不必创建任何新目录。
有什么想法吗?
TIA,
Benjy
P.S.:使用 ImageField
不太可行,因为我需要内联显示图像。谢谢!
I am writing a Drupal module where users send down content (formatted as HTML). The content will almost always include <img>
tags. Those tags will reference the user's server. But, I don't want to hit the user's server every time that content is requested; I want to serve up a version on my server.
So, I have written a function to look through the HTML for <img>
tags and then use cURL to save them locally. All is well - except I can't figure out where in my local filesystem to store the images, thanks to Drupal's crazy URL-rewriting. Ideally I could put them in a directory that is already accessible over the web and not have to create any new directories.
Any thoughts?
TIA,
Benjy
P.S.: Not really feasible to use an ImageField
as I need to display the image inline. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
理论上,您应该能够将它们放在 webroot 下的任何位置,并且它们将是可用的,但通常情况下,内容图像(而不是主题图像)位于您的
files
目录中,该目录通常是配置为/sites/default/files
。我通常会放置一个子文件夹来添加一些结构,因此我会将它们放在/sites/default/files/images/scraped
(或其他位置)中。注意:您可以使用 file_directory_path 获取文件路径。您还可以查看文件处理 api。
In theory, you should be able to put them anywhere under the webroot, and they'll be servable, but normally, content images (as opposed to theme images) go in your
files
directory, which is usually configured as/sites/default/files
. I usually, put a sub-folder to add some structure, so I'd put them in/sites/default/files/images/scraped
(or whatever).Note: you can get the file path with file_directory_path. You might also look at the file handling api.
为什么不把它放在drupal目录中?默认情况下,站点/默认/文件
why you don't put it in drupal directory? by default, sites/default/files