diff --git a/core/includes/file.inc b/core/includes/file.inc
index 16ed619..520bbcb 100644
--- a/core/includes/file.inc
+++ b/core/includes/file.inc
@@ -1421,6 +1421,8 @@ function file_download() {
  *     extension. Defaults to 'uri'.
  *   - 'min_depth': Minimum depth of directories to return files from. Defaults
  *     to 0.
+ *   - 'max_depth': Maximum depth of directories to return files form. Defaults
+ *     to unlimited.
  * @param $depth
  *   Current depth of recursion. This parameter is only used internally and
  *   should not be passed in.
@@ -1430,6 +1432,12 @@ function file_download() {
  *   'filename', and 'name' members corresponding to the matching files.
  */
 function file_scan_directory($dir, $mask, $options = array(), $depth = 0) {
+  // Check if this function should run.
+  if (isset($options['max_depth']) && $options['max_depth'] < $depth) {
+    // No? Then immediately return.
+    return array();
+  }
+
   // Merge in defaults.
   $options += array(
     'nomask' => '/(\.\.?|CVS)$/',
@@ -1444,7 +1452,7 @@ function file_scan_directory($dir, $mask, $options = array(), $depth = 0) {
   // Avoid warnings when opendir does not have the permissions to open a
   // directory.
   if (is_dir($dir)) {
-    if($handle = @opendir($dir)) {
+    if ($handle = @opendir($dir)) {
       while (FALSE !== ($filename = readdir($handle))) {
         if (!preg_match($options['nomask'], $filename) && $filename[0] != '.') {
           $uri = "$dir/$filename";
diff --git a/core/modules/system/lib/Drupal/system/Tests/File/ScanDirectoryTest.php b/core/modules/system/lib/Drupal/system/Tests/File/ScanDirectoryTest.php
index b4a4885..436c96a 100644
--- a/core/modules/system/lib/Drupal/system/Tests/File/ScanDirectoryTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/File/ScanDirectoryTest.php
@@ -70,7 +70,7 @@ function testOptionCallback() {
     file_test_file_scan_callback_reset();
     $this->assertEqual(0, count($results), 'No files were passed to the callback.');
 
-    // Grab a listing of all the JavaSscript files and check that they're
+    // Grab a listing of all the Javascript files and check that they're
     // passed to the callback.
     $all_files = file_scan_directory($this->path, '/^javascript-/', array('callback' => 'file_test_file_scan_callback'));
     $this->assertEqual(2, count($all_files), 'Found two, expected javascript files.');
@@ -132,10 +132,8 @@ function testOptionRecurse() {
     $this->assertEqual(2, count($files), 'With recursion we found the expected javascript files.');
   }
 
-
   /**
-   * Check that the min_depth options lets us ignore files in the starting
-   * directory.
+   * Check that the min_depth options lets us ignore files in the starting dir.
    */
   function testOptionMinDepth() {
     $files = file_scan_directory($this->path, '/^javascript-/', array('min_depth' => 0));
@@ -144,4 +142,14 @@ function testOptionMinDepth() {
     $files = file_scan_directory($this->path, '/^javascript-/', array('min_depth' => 1));
     $this->assertTrue(empty($files), 'Minimum-depth of 1 successfully excludes files from current directory.');
   }
+
+  /**
+   * Checks that the max_depth options lets us ignore child directories.
+   */
+  function testOptionMaxDepth() {
+    // There are only CSS files in the 'css_test_files' directory. A max depth
+    // of zero (zero children down) will not show any of these.
+    $files = file_scan_directory($this->path, '/css/', array('max_depth' => 0));
+    $this->assertEqual(0, count($files), 'Maximum depth limits files found');
+  }
 }
