You are here

function file_check_directory in Drupal 6

Same name and namespace in other branches
  1. 4 includes/file.inc \file_check_directory()
  2. 5 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

9 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.
file_check_path in includes/file.inc
Checks path to see if it is a directory, or a dir/file.
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…

... 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;
}