wp_mkdir_p()新建绝对路径的递归式文件夹

目录

描述

译文

新建绝对路径的递归式文件夹。

试图设置文件夹的访问权限。

原文

Recursive directory creation based on full path.

Will attempt to set permissions on folders.

wp_mkdir_p() 描述

用法

<?php wp_mkdir_p$target ?>

wp_mkdir_p() 用法

参数

$target

(string) (必填) Full path to attempt to create.

默认值: None

wp_mkdir_p() 参数

返回值

(boolean) 

Whether the path was created or not. True if path already exists.

wp_mkdir_p() 返回值

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php
if (wp_mkdir_p('/a/really/deep/sub/directory')) {
  echo 'It worked! Now look for a directory named "a".';
}
?>

wp_mkdir_p() 示例

历史

添加于 版本: 2.0.1

wp_mkdir_p() 历史

源文件

wp_mkdir_p() 函数的代码位于 wp-includes/functions.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Recursive directory creation based on full path.
 *
 * Will attempt to set permissions on folders.
 *
 * @since 2.0.1
 *
 * @param string $target Full path to attempt to create.
 * @return bool Whether the path was created. True if path already exists.
 */
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 ( $wrapper !== null ) {
		$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 );

	// 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 ) ) {
		$target_parent = dirname( $target_parent );
	}

	// Get the permission bits.
	if ( $stat = @stat( $target_parent ) ) {
		$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 != ( $dir_perms & ~umask() ) ) {
			$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;="" }="">

wp_mkdir_p() 源文件