php+文件夹不存在+创建文件夹,php - 创建文件夹(如果尚不存在) - 堆栈内存溢出...

在WordPress中,还有一个非常方便的函数wp_mkdir_p ,它将以递归方式创建目录结构。

资料来源:-

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.

$target = rtrim($target, '/'); // Use rtrim() instead of untrailingslashit to avoid formatting.php dependency.

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; $i <= count( $folder_parts ); $i++ ) {

@chmod( $target_parent . '/' . implode( '/', array_slice( $folder_parts, 0, $i ) ), $dir_perms );

}

}

return true;

}

return false;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值