Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By markj on
Hi,
Is there a way to determine the server filesystem path to the drupal install directory? I am writing a module that reads the fienames of all images in a subdirectory in 'files' (i.e., the value of the file_directory_path system variable) and formats HTML for displaying the images -- a very simple image gallery generator that allows you to upload a bunch of images via FTP. However, I can't figure out how to get the filesystem path to the files directory. Is there a way to programmatically generate this? Or do I have to have the user set it in the module settings?
<?php
$path_base_dir = '/path/to/my/files/directory/'; // <- This is what I want to determine programatically -- or do I have to store it in variables table using variable_set and variable_get?
$data_dir = 'my_data_dir/';
$path_data_dir = $path_base_dir . $data_dir;
$files = file_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse = TRUE, $key = 'filename', $min_depth = 0, $depth = 0);
foreach ($files as $file) {
$file->name . '</p>';
}
?>
TIA, Mark
Comments
base_path() is your friend.
Your file system path should be $_SERVER['DOCUMENT_ROOT'] . base_path();
However, in your example, this path isn't needed. file_scan_directory() starts at the top of the document root and searches below that.
An example:
If you have the files in your default "files" directory named "a.css" "b.css" and "c.css", then this snippet:
$a = file_scan_directory('files', 'css');
will get you a listing of a.css, b.css and c.css
BTW, if you're asking, "why respond a question over a year old," my answer would be, "because this page is the first listing for the google query for 'drupal determining system file path'." Best to have an answer for the next person looking.
...and I was that next person
...and I was that next person. Thanks kitt
And i was the third. Good
And i was the third. Good work!
xtfer.com
platform.sh
or do this...
$_SERVER['DOCUMENT_ROOT'] . base_path().file_directory_path()
The Drupal functions below
The Drupal functions below will help:
http://api.drupal.org/api/function/file_directory_path/5
http://api.drupal.org/api/function/base_path/5
__________________________________________________________
Mark Theunissen
Code Baboon
Drupal based services
Thanks
file_directory_path() is what I was looking for. I needed to define some theme functions to look in the files directory of the site, not in the theme directory.
How can I convert a file uri to relative path
See: #837794: How can I convert a file uri to relative path?
http://drupal.stackexchange.com/questions/45299/why-drupal-realpathpubli...
FOR DRUPAL 7/8
file_directory_path() has been removed, so use this method now:
example:
<img src="<?php echo file_create_url(file_default_scheme()."://"); ?>/my-image.png" />