Same name and namespace in other branches
  1. 4.6.x includes/file.inc \file_check_directory()
  2. 4.7.x includes/file.inc \file_check_directory()
  3. 5.x includes/file.inc \file_check_directory()

Checks whether a directory exists and is writable.

Furthermore, the directory can optionally be created if it does not exist, and/or be set to writable if it is currently not. Directories need to have execute permission to be considered a directory by FTP servers.

Parameters

$directory: A string representing the directory path.

$mode: An optional bitmask containing the actions, if any, to be carried out on the directory. Any combination of the actions FILE_CREATE_DIRECTORY and FILE_MODIFY_PERMISSIONS is allowed.

$form_item: An optional string containing the name of a form item that any errors will be attached to. Useful when the function validates a directory path entered as a form value. An error will consequently prevent form submit handlers from running, and instead display the form along with the error messages.

Return value

FALSE if the directory does not exist or is not writable, even after any optional actions have been carried out. Otherwise, TRUE is returned.

Related topics

7 calls to file_check_directory()
color_scheme_form_submit in modules/color/color.module
Submit handler for color change form.
drupal_build_css_cache in includes/common.inc
Aggregate and optimize CSS files, putting them in the files directory.
drupal_build_js_cache in includes/common.inc
Aggregate JS files, putting them in the files directory.
system_check_directory in modules/system/system.module
Checks the existence of the directory specified in $form_element. This function is called from the system_settings form to check both the file_directory_path and file_directory_temp directories. If validation fails, the form element is flagged with an…
system_theme_settings in modules/system/system.admin.inc
Form builder; display theme configuration for entire site and individual themes.

... See full list

File

includes/file.inc, line 108
API for handling file uploads and server file management.

Code

function file_check_directory(&$directory, $mode = 0, $form_item = NULL) {
  $directory = rtrim($directory, '/\\');

  // Check if directory exists.
  if (!is_dir($directory)) {
    if ($mode & FILE_CREATE_DIRECTORY && @mkdir($directory)) {
      drupal_set_message(t('The directory %directory has been created.', array(
        '%directory' => $directory,
      )));
      @chmod($directory, 0775);

      // Necessary for non-webserver users.
    }
    else {
      if ($form_item) {
        form_set_error($form_item, t('The directory %directory does not exist.', array(
          '%directory' => $directory,
        )));
      }
      return FALSE;
    }
  }

  // Check to see if the directory is writable.
  if (!is_writable($directory)) {
    if ($mode & FILE_MODIFY_PERMISSIONS && @chmod($directory, 0775)) {
      drupal_set_message(t('The permissions of directory %directory have been changed to make it writable.', array(
        '%directory' => $directory,
      )));
    }
    else {
      form_set_error($form_item, t('The directory %directory is not writable', array(
        '%directory' => $directory,
      )));
      watchdog('file system', 'The directory %directory is not writable, because it does not have the correct permissions set.', array(
        '%directory' => $directory,
      ), WATCHDOG_ERROR);
      return FALSE;
    }
  }
  if (file_directory_path() == $directory || file_directory_temp() == $directory) {
    file_create_htaccess($directory, $form_item);
  }
  return TRUE;
}