wppaste
WordPress

wp_mkdir_p( string $target ): bool

Since
2.0.1
Source
wp-includes/functions.php:2042
Recursive directory creation based on full path.

Description

Will attempt to set permissions on folders.

Compatibility

WordPress
since 2.0.1
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$targetstring
Full path to attempt to create.

Return value

bool
Whether the path was created. True if path already exists.

Performance profile

How much work a call to wp_mkdir_p() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reads or writes the filesystem via file_exists().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
25–88

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 112.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
7

7 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • filesystemfilesystem accessfile_exists()called directly

What one call costs · 16 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_mkdir_p() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!wp_is_stream() && !file_exists() && $target25–31wp_is_stream(), rtrim(), file_exists()
!wp_is_stream() && file_exists()28–32wp_is_stream(), rtrim(), file_exists(), is_dir()
wp_is_stream() && !file_exists() && $target34–40wp_is_stream(), explode(), rtrim(), file_exists()
wp_is_stream() && file_exists()37–41wp_is_stream(), explode(), rtrim(), file_exists(), is_dir()
!wp_is_stream() && !file_exists() && !$target && $target_parent === "." && !mkdir()47–53wp_is_stream(), rtrim(), file_exists(), stat(), mkdir()
!wp_is_stream() && !file_exists() && !$target && $target_parent !== "." && !mkdir()51–60wp_is_stream(), rtrim(), file_exists(), is_dir(), stat(), mkdir()
!wp_is_stream() && !file_exists() && !$target && $target_parent === "." && mkdir() && $dir_perms === false53–59wp_is_stream(), rtrim(), file_exists(), stat(), mkdir(), umask()
wp_is_stream() && !file_exists() && !$target && $target_parent === "." && !mkdir()56–62wp_is_stream(), explode(), rtrim(), file_exists(), stat(), mkdir()
!wp_is_stream() && !file_exists() && !$target && $target_parent !== "." && mkdir() && $dir_perms === false57–66wp_is_stream(), rtrim(), file_exists(), is_dir(), stat(), mkdir(), umask()
wp_is_stream() && !file_exists() && !$target && $target_parent !== "." && !mkdir()60–69wp_is_stream(), explode(), rtrim(), file_exists(), is_dir(), stat(), mkdir()
wp_is_stream() && !file_exists() && !$target && $target_parent === "." && mkdir() && $dir_perms === false62–68wp_is_stream(), explode(), rtrim(), file_exists(), stat(), mkdir(), umask()
!wp_is_stream() && !file_exists() && !$target && $target_parent === "." && mkdir() && $dir_perms !== false && !$i66–72wp_is_stream(), rtrim(), file_exists(), stat(), mkdir(), umask(), explode()
4 further outcomes, up to 88 instructions
wp_is_stream() && !file_exists() && !$target && $target_parent !== "." && mkdir() && $dir_perms === false66–75wp_is_stream(), explode(), rtrim(), file_exists(), is_dir(), stat(), mkdir(), umask()
!wp_is_stream() && !file_exists() && !$target && $target_parent !== "." && mkdir() && $dir_perms !== false && !$i70–79wp_is_stream(), rtrim(), file_exists(), is_dir(), stat(), mkdir(), umask(), explode()
wp_is_stream() && !file_exists() && !$target && $target_parent === "." && mkdir() && $dir_perms !== false && !$i75–81wp_is_stream(), explode(), rtrim(), file_exists(), stat(), mkdir(), umask(), explode()
wp_is_stream() && !file_exists() && !$target && $target_parent !== "." && mkdir() && $dir_perms !== false && !$i79–88wp_is_stream(), explode(), rtrim(), file_exists(), is_dir(), stat(), mkdir(), umask(), explode()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11225–8813
8.511225–8813
8.411225–881322 fewer instructions than PHP 8.3
8.313431–10513
8.213431–10513
8.113431–10513
7.413431–10513

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 2

Used by · 7

Source code

function wp_mkdir_p( $target ) {	$wrapper = null; 	// Strip the protocol.	if ( wp_is_stream( $target ) ) {		list( $wrapper, $target ) = explode( '://', $target, 2 );	} 	// From php.net/mkdir user contributed notes.	$target = str_replace( '//', '/', $target ); 	// Put the wrapper back on the target.	if ( null !== $wrapper ) {		$target = $wrapper . '://' . $target;	} 	/*	 * Safe mode fails with a trailing slash under certain PHP versions.	 * Use rtrim() instead of untrailingslashit to avoid formatting.php dependency.	 */	$target = rtrim( $target, '/' );	if ( empty( $target ) ) {		$target = '/';	} 	if ( file_exists( $target ) ) {		return @is_dir( $target );	} 	// Do not allow path traversals.	if ( str_contains( $target, '../' ) || str_contains( $target, '..' . DIRECTORY_SEPARATOR ) ) {		return false;	} 	// We need to find the permissions of the parent folder that exists and inherit that.	$target_parent = dirname( $target );	while ( '.' !== $target_parent && ! is_dir( $target_parent ) && dirname( $target_parent ) !== $target_parent ) {		$target_parent = dirname( $target_parent );	} 	// Get the permission bits.	$stat = @stat( $target_parent );	if ( $stat ) {		$dir_perms = $stat['mode'] & 0007777;	} else {		$dir_perms = 0777;	} 	if ( @mkdir( $target, $dir_perms, true ) ) { 		/*		 * If a umask is set that modifies $dir_perms, we'll have to re-set		 * the $dir_perms correctly with chmod()		 */		if ( ( $dir_perms & ~umask() ) !== $dir_perms ) {			$folder_parts = explode( '/', substr( $target, strlen( $target_parent ) + 1 ) );			for ( $i = 1, $c = count( $folder_parts ); $i <= $c; $i++ ) {				chmod( $target_parent . '/' . implode( '/', array_slice( $folder_parts, 0, $i ) ), $dir_perms );			}		} 		return true;	} 	return false;}

Changelog

Introduced in 2.0.1. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.