Skip navigation


6 drupal_get_filename($type, $name, $filename = NULL)
7 drupal_get_filename($type, $name, $filename = NULL)

Returns and optionally sets the filename for a system item (module, theme, etc.). The filename, whether provided, cached, or retrieved from the database, is only returned if the file exists.

This function plays a key role in allowing Drupal's resources (modules and themes) to be located in different places depending on a site's configuration. For example, a module 'foo' may legally be be located in any of these three places:

modules/foo/foo.module sites/all/modules/foo/foo.module sites/

Calling drupal_get_filename('module', 'foo') will give you one of the above, depending on where the module is located.


$type: The type of the item (i.e. theme, theme_engine, module, profile).

$name: The name of the item for which the filename is requested.

$filename: The filename of the item if it is to be set explicitly rather than by consulting the database.

Return value

The filename of the requested item.

6 calls to drupal_get_filename()

1 string reference to 'drupal_get_filename'


drupal/includes/, line 635
Functions that need to be loaded on every Drupal request.


function drupal_get_filename($type, $name, $filename = NULL) {
  // The location of files will not change during the request, so do not use
  // drupal_static().
  static $files = array();

  if (!isset($files[$type])) {
    $files[$type] = array();

  if (!empty($filename) && file_exists($filename)) {
    $files[$type][$name] = $filename;
  elseif (isset($files[$type][$name])) {
    // nothing
  // Verify that we have an active database connection, before querying
  // the database. This is required because this function is called both
  // before we have a database connection (i.e. during installation) and
  // when a database connection fails.
  else {
    try {
      if (function_exists('db_query')) {
        $file = db_query("SELECT filename FROM {system} WHERE name = :name AND type = :type", array(':name' => $name, ':type' => $type))->fetchField();
        if (file_exists(DRUPAL_ROOT . '/' . $file)) {
          $files[$type][$name] = $file;
    catch (Exception $e) {
      // The database table may not exist because Drupal is not yet installed,
      // or the database might be down. We have a fallback for this case so we
      // hide the error completely.
    // Fallback to searching the filesystem if the database could not find the
    // file or the file returned by the database is not found.
    if (!isset($files[$type][$name])) {
      // We have a consistent directory naming: modules, themes...
      $dir = $type . 's';
      if ($type == 'theme_engine') {
        $dir = 'themes/engines';
        $extension = 'engine';
      elseif ($type == 'theme') {
        $extension = 'info';
      else {
        $extension = $type;

      if (!function_exists('drupal_system_listing')) {
        require_once DRUPAL_ROOT . '/includes/';
      // Scan the appropriate directories for all files with the requested
      // extension, not just the file we are currently looking for. This
      // prevents unnecessary scans from being repeated when this function is
      // called more than once in the same page request.
      $matches = drupal_system_listing("/^" . DRUPAL_PHP_FUNCTION_PATTERN . "\.$extension$/", $dir, 'name', 0);
      foreach ($matches as $matched_name => $file) {
        $files[$type][$matched_name] = $file->uri;

  if (isset($files[$type][$name])) {
    return $files[$type][$name];