1 file.inc file_prepare_directory(&$directory, $options = FILE_MODIFY_PERMISSIONS)

Checks that the directory exists and is writable.

Directories need to have execute permissions to be considered a directory by FTP servers, etc.


$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


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


function file_prepare_directory(&$directory, $options = FILE_MODIFY_PERMISSIONS) {
  $scheme = file_uri_scheme($directory);
  if (!$scheme || !file_stream_wrapper_valid_scheme($scheme)) {
    // 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) && @backdrop_mkdir($directory, NULL, TRUE)) {
      return backdrop_chmod($directory);
    // Race conditions could lead to the directory having been created by
    // another request. Check if the directory exists again after failing.
    // See https://github.com/backdrop/backdrop-issues/issues/5631
    return is_dir($directory) && is_writable($directory);
  // The directory exists, so check to see if it is writable.
  $writable = is_writable($directory);
  if (!$writable && ($options & FILE_MODIFY_PERMISSIONS)) {
    return backdrop_chmod($directory);

  return $writable;