You are here

function file_prepare_directory in Drupal 7

Same name and namespace in other branches
  1. 8 core/includes/file.inc \file_prepare_directory()

Checks 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 reference containing the name of a directory path or URI. A trailing slash will be trimmed from a path.

$options: A bitmask to indicate if the directory should be created if it does not exist (FILE_CREATE_DIRECTORY) or made writable if it is read-only (FILE_MODIFY_PERMISSIONS).

Return value

TRUE if the directory exists (or was created) and is writable. FALSE otherwise.

Related topics

24 calls to file_prepare_directory()
color_scheme_form_submit in modules/color/color.module
Form submission handler for color_scheme_form().
DrupalUnitTestCase::setUp in modules/simpletest/drupal_web_test_case.php
Sets up unit test environment.
DrupalWebTestCase::prepareEnvironment in modules/simpletest/drupal_web_test_case.php
Prepares the current environment for running the test.
drupal_build_css_cache in includes/common.inc
Aggregates and optimizes CSS files into a cache file in the files directory.
drupal_build_js_cache in includes/common.inc
Aggregates JavaScript files into a cache file in the files directory.

... See full list

File

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

Code

function file_prepare_directory(&$directory, $options = FILE_MODIFY_PERMISSIONS) {
  if (!file_stream_wrapper_valid_scheme(file_uri_scheme($directory))) {

    // Only trim if we're not dealing with a stream.
    $directory = rtrim($directory, '/\\');
  }

  // Check if directory exists.
  if (!is_dir($directory)) {

    // Let mkdir() recursively create directories and use the default directory
    // permissions.
    if ($options & FILE_CREATE_DIRECTORY && @drupal_mkdir($directory, NULL, TRUE)) {
      return drupal_chmod($directory);
    }
    return FALSE;
  }

  // The directory exists, so check to see if it is writable.
  $writable = is_writable($directory);
  if (!$writable && $options & FILE_MODIFY_PERMISSIONS) {
    return drupal_chmod($directory);
  }
  return $writable;
}