How can I scan a directory and its files from a module? Is there any permission issue? I have to apply some logic based on the folder structure in the active theme.

When I use the below code, its returning empty


I am getting the path to theme by below code:

$path = base_path() . drupal_get_path('theme','sandesh').'/'.'layouts';

Is this the wrong method to this? Or is there any other apis for that.

  • I have also tried using scandir php function instead of file_scan_directory . That is showing error . Jun 24, 2012 at 11:16
  • 2
    Just a hint: Have a look how drupal core or other contrib modules doing it. Check drupal_get_path or module_invoke_all are finding their files. The variables module is also looking for *.variable.inc files in module directories. I often learn from other modules how to do things.
    – BetaRide
    Jul 21, 2012 at 8:28

2 Answers 2


You are mixing filesystem path and URL path:

  • base_path(): Returns the base URL path (i.e., directory) of the Drupal installation.
  • drupal_get_path(): Returns the path to a system item (module, theme, etc.).

The second argument for file_scan_directory() is the preg_match() regular expression of the files to find. You are using '0' which only match a single file named '0'.

file_scan_directory(DRUPAL_ROOT . '/' . drupal_get_path('theme','sandesh') . '/' . 'layouts'); is all you need to do. Use base_path() only when building URL for which an existing helper function (url(), file_create_url(), etc.) cannot be used.


I think your problem has to do with using web paths instead of filesystem paths.

$_SERVER['DOCUMENT_ROOT'] will have the path that your sites is stored in on the server, eg /var/www/html. You should then be able to concatenate this with the results from drupal_get_path to get the read directory. You will likely need to play with adding / at places in the path.

You may also be able to use DRUPAL_ROOT instead of $_SERVER['DOCUMENT_ROOT'], but I don't quite recall if they are always equivalent. You could also potentially use __DIR__ from within the theme or module.

  • I tried using $_SERVER['DOCUMENT_ROOT'] and printed the path , the result is 'D:/xampp/htdocs/sites/all/themes/sandesh/layouts/dir', but still 'scandir($path)' shows error and returns false value . Jun 24, 2012 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.