| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Recursive directory creation based on full path. |
|---|
| 5 | * |
|---|
| 6 | * Will attempt to set permissions on folders. |
|---|
| 7 | * |
|---|
| 8 | * @since 2.0.1 |
|---|
| 9 | * |
|---|
| 10 | * @param string $target Full path to attempt to create. |
|---|
| 11 | * @return bool Whether the path was created. True if path already exists. |
|---|
| 12 | */ |
|---|
| 13 | function wp_mkdir_p( $target ) { |
|---|
| 14 | // from php.net/mkdir user contributed notes |
|---|
| 15 | $target = str_replace( '//', '/', $target ); |
|---|
| 16 | |
|---|
| 17 | // safe mode fails with a trailing slash under certain PHP versions. |
|---|
| 18 | $target = rtrim($target, '/'); // Use rtrim() instead of untrailingslashit to avoid formatting.php dependency. |
|---|
| 19 | if ( empty($target) ) |
|---|
| 20 | $target = '/'; |
|---|
| 21 | |
|---|
| 22 | if ( file_exists( $target ) ) |
|---|
| 23 | return @is_dir( $target ); |
|---|
| 24 | |
|---|
| 25 | // Attempting to create the directory may clutter up our display. |
|---|
| 26 | if ( @mkdir( $target ) ) { |
|---|
| 27 | $stat = @stat( dirname( $target ) ); |
|---|
| 28 | $dir_perms = $stat['mode'] & 0007777; // Get the permission bits. |
|---|
| 29 | @chmod( $target, $dir_perms ); |
|---|
| 30 | return true; |
|---|
| 31 | } elseif ( is_dir( dirname( $target ) ) ) { |
|---|
| 32 | return false; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | // If the above failed, attempt to create the parent node, then try again. |
|---|
| 36 | if ( ( $target != '/' ) && ( wp_mkdir_p( dirname( $target ) ) ) ) |
|---|
| 37 | return wp_mkdir_p( $target ); |
|---|
| 38 | |
|---|
| 39 | return false; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | wp_mkdir_p( dirname(__FILE__) . '/1/2/3/4/' ); |
|---|
| 43 | ?> |
|---|