diff --git a/core/includes/file.inc b/core/includes/file.inc index 5c4a75a8884..c875bdc546d 100644 --- a/core/includes/file.inc +++ b/core/includes/file.inc @@ -1475,8 +1475,19 @@ function drupal_mkdir($uri, $mode = NULL, $recursive = FALSE, $context = NULL) { $uri = str_replace('/', DIRECTORY_SEPARATOR, $uri); // Determine the components of the path. $components = explode(DIRECTORY_SEPARATOR, $uri); + // If the filepath is absolute the first component will be empty as there + // will be nothing before the first slash. + if ($components[0] == '') { + $recursive_path = DIRECTORY_SEPARATOR; + // Get rid of the empty first component. + array_shift($components); + } + else { + $recursive_path = ''; + } + // Don't handle the top-level directory in this loop. array_pop($components); - $recursive_path = ''; + // Create each component if necessary. foreach ($components as $component) { $recursive_path .= $component; diff --git a/core/modules/system/lib/Drupal/system/Tests/File/DirectoryTest.php b/core/modules/system/lib/Drupal/system/Tests/File/DirectoryTest.php index 9a7a8120fe6..f72430fdbdd 100644 --- a/core/modules/system/lib/Drupal/system/Tests/File/DirectoryTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/File/DirectoryTest.php @@ -50,6 +50,11 @@ class DirectoryTest extends FileTestBase { // Check that existing directory permissions were not modified. $this->assertDirectoryPermissions($directory, $old_mode); + + // Check creating a directory using an absolute path. + $absolute_path = drupal_realpath($directory) . DIRECTORY_SEPARATOR . $this->randomName() . DIRECTORY_SEPARATOR . $this->randomName(); + $this->assertTrue(drupal_mkdir($absolute_path, 0775, TRUE), 'No error reported when creating new absolute directories.', 'File'); + $this->assertDirectoryPermissions($absolute_path, 0775); } /**