function drupal_chmod

You are here

7 file.inc drupal_chmod($uri, $mode = NULL)
8 file.inc drupal_chmod($uri, $mode = NULL)

Sets the permissions on a file or directory.

This function will use the file_chmod_directory and file_chmod_file settings for the default modes for directories and uploaded/generated files. By default these will give everyone read access so that users accessing the files with a user account without the webserver group (e.g. via FTP) can read these files, and give group write permissions so webserver group members (e.g. a vhost account) can alter files uploaded and owned by the webserver.

PHP's chmod does not support stream wrappers so we use our wrapper implementation which interfaces with chmod() by default. Contrib wrappers may override this behavior in their implementations as needed.

Parameters

$uri: A string containing a URI file, or directory path.

$mode: Integer value for the permissions. Consult PHP chmod() documentation for more information.

Return value

bool TRUE for success, FALSE in the event of an error.

Related topics

11 calls to drupal_chmod()
DirectoryTest::testFileCheckDirectoryHandling in core/modules/system/lib/Drupal/system/Tests/File/DirectoryTest.php
Test directory handling functions.
FileStorage::write in core/lib/Drupal/Core/Config/FileStorage.php
Writes configuration data to the storage.
file_prepare_directory in core/includes/file.inc
Checks that the directory exists and is writable.
file_save_htaccess in core/includes/file.inc
Creates a .htaccess file in the given directory.
file_save_upload in core/modules/file/file.module
Saves file uploads to a new location.

... See full list

File

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

Code

function drupal_chmod($uri, $mode = NULL) {
  if (!isset($mode)) {
    if (is_dir($uri)) {
      $mode = Settings::get('file_chmod_directory', FILE_CHMOD_DIRECTORY);
    }
    else {
      $mode = Settings::get('file_chmod_file', FILE_CHMOD_FILE);
    }
  }

  if (@chmod($uri, $mode)) {
    return TRUE;
  }

  watchdog('file', 'The file permissions could not be set on %uri.', array('%uri' => $uri), WATCHDOG_ERROR);
  return FALSE;
}