How can I programmatically store an image in a publicly accessible location on my Drupal site?

StackOverflow https://stackoverflow.com/questions/2143318

문제

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!

도움이 되었습니까?

해결책

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.

다른 팁

why you don't put it in drupal directory? by default, sites/default/files

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top