php中mkdir函数递归创建目录(文件夹)
在php中mkdir函数是创建目录它自身是不可以递归创建目录了,我们需要遍历进行目录然后调用mkdir即可,下面来看一下操作方法。
例
<?php /** * Makes directory and returns BOOL(TRUE) if exists OR made. * * @param $path Path name * @return bool */ function rmkdir($path, $mode = 0755) { $path = rtrim(preg_replace(array( "/\/", "//{2,}/" ) , "/", $path) , "/"); $e = explode("/", ltrim($path, "/")); if (substr($path, 0, 1) == "/") { $e[0] = "/" . $e[0]; } $c = count($e); $cp = $e[0]; for ($i = 1; $i < $c; $i++) { if (!is_dir($cp) && !@mkdir($cp, $mode)) { return false; } $cp.= "/" . $e[$i]; } return @mkdir($path, $mode); } ?>
例2
Somehow the recursive version of mkdir didn't work for me on Mac and the workaraounds listed
below alsow didn't work for me, so heres my solution:
<?php function mkdir_r($dirName, $rights = 0777) { $dirs = explode('/', $dirName); $dir = ''; foreach ($dirs as $part) { $dir.= $part . '/'; if (!is_dir($dir) && strlen($dir) > 0) mkdir($dir, $rights); } } ?>
Tested and works ;)
例3
<?php function mkdirs($dir) { if (!is_dir($dir)) { if (!mkdirs(dirname($dir))) { return false; } if (!mkdir($dir, 0777)) { return false; } } return true; } function rmdirs($dir) { $d = dir($dir); while (false !== ($child = $d->read())) { if ($child != '.' && $child != '..') { if (is_dir($dir . '/' . $child)) rmdirs($dir . '/' . $child); else unlink($dir . '/' . $child); } } $d->close(); rmdir($dir); } ?>
三个创建目录的函数都有自己的优点,这里我也没有一个个测试了,只使用了第二个感觉非常的不错。
永久链接:
转载随意!带上文章地址吧。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. The difference between Iterator and foreach is the polymorphic difference (the bottom layer of foreach is Iterator) Iterator is an interface type, it does not care about the type of collection or array; both for and foreach need to know the type of collection first, even the type of elements in the collection; 1. Why is it said that the bottom layer of foreach is the code written by Iterator: Decompiled code: 2. The difference between remove in foreach and iterator. First, look at the Alibaba Java Development Manual, but no error will be reported in case 1, and an error will be reported in case 2 (java. util.ConcurrentModificationException) first

The steps for PHP to determine the number of the foreach loop: 1. Create an array of "$fruits"; 2. Create a counter variable "$counter" with an initial value of 0; 3. Use "foreach" to loop through the array, and Increase the value of the counter variable in the loop body, and then output each element and their index; 4. Output the value of the counter variable outside the "foreach" loop to confirm which element the loop reaches.

This article will explain in detail how PHP returns an array after key value flipping. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Key Value Flip Array Key value flip is an operation on an array that swaps the keys and values in the array to generate a new array with the original key as the value and the original value as the key. Implementation method In PHP, you can perform key-value flipping of an array through the following methods: array_flip() function: The array_flip() function is specially used for key-value flipping operations. It receives an array as argument and returns a new array with the keys and values swapped. $original_array=[

The explode function in PHP is a function used to split a string into an array. It is very common and flexible. In the process of using the explode function, we often encounter some errors and problems. This article will introduce the basic usage of the explode function and provide some methods to solve the error reports. 1. Basic usage of the explode function In PHP, the basic syntax of the explode function is as follows: explode(string$separator,string$stri

Title: Common errors and solutions when using the explode function in PHP In PHP, the explode function is a common function used to split a string into an array. However, some common errors can occur due to improper use or incorrect data format. This article will analyze the problems you may encounter when using the explode function, and provide solutions and specific code examples. Mistake 1: The delimiter parameter is not passed in. When using the explode function, one of the most common mistakes is that the delimiter parameter is not passed in.

This article will explain in detail the ASCII value of the first character of the string returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP returns the ASCII value of the first character of a string Introduction In PHP, getting the ASCII value of the first character of a string is a common operation that involves basic knowledge of string processing and character encoding. ASCII values are used to represent the numeric value of characters in computer systems and are critical for character comparison, data transmission and storage. The process of getting the ASCII value of the first character of a string involves the following steps: Get String: Determine the string for which you want to get the ASCII value. It can be a variable or a string constant

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

In PHP programming, processing strings is a frequently required operation. Among them, splitting and merging strings are two common requirements. In order to perform these operations more conveniently, PHP provides two very practical functions, namely the explode and implode functions. This article will introduce the usage of these two functions, as well as some practical skills. 1. The explode function The explode function is used to split a string according to the specified delimiter and return an array. Its function prototype is as follows: arra
