You are here

function file_check_directory in Drupal 4

Same name and namespace in other branches
  1. 5 includes/file.inc \file_check_directory()
  2. 6 includes/file.inc \file_check_directory()

Check that the directory exists and is writable. Directories need to have execute permissions to be considered a directory by FTP servers, etc.

Parameters

$directory A string containing the name of a directory path.:

$mode A Boolean value to indicate if the directory should be created: if it does not exist or made writable if it is read-only.

$form_item An optional string containing the name of a form item that: any errors will be attached to. This is useful for settings forms that require the user to specify a writable directory. If it can't be made to work, a form error will be set preventing them from saving the settings.

Return value

False when directory not found, or true when directory exists.

Related topics

4 calls to file_check_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.module
Checks the existence of the directory specified in $form_element. This function is called from the system_view_general form to check both the file_directory_path and file_directory_temp directories. If validation fails, the form element is flagged…
system_theme_settings in modules/system.module
Menu callback; display theme configuration for entire site and individual themes.
user_configure in modules/user.module

File

includes/file.inc, line 85
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' => theme('placeholder', $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' => theme('placeholder', $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' => theme('placeholder', $directory),
      )));
    }
    else {
      form_set_error($form_item, t('The directory %directory is not writable', array(
        '%directory' => theme('placeholder', $directory),
      )));
      watchdog('file system', t('The directory %directory is not writable, because it does not have the correct permissions set.', array(
        '%directory' => theme('placeholder', $directory),
      )), WATCHDOG_ERROR);
      return false;
    }
  }
  if ((file_directory_path() == $directory || file_directory_temp() == $directory) && !is_file("{$directory}/.htaccess")) {
    $htaccess_lines = "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\nOptions None\nOptions +FollowSymLinks";
    if (($fp = fopen("{$directory}/.htaccess", 'w')) && fputs($fp, $htaccess_lines)) {
      fclose($fp);
      chmod($directory . '/.htaccess', 0664);
    }
    else {
      $message = t("Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines: <code>%htaccess</code>", array(
        '%directory' => theme('placeholder', $directory),
        '%htaccess' => '<br />' . str_replace("\n", '<br />', check_plain($htaccess_lines)),
      ));
      form_set_error($form_item, $message);
      watchdog('security', $message, WATCHDOG_ERROR);
    }
  }
  return true;
}