PHP Create level directory, multilevel directory
Uploading files often requires different types of files to be placed in different folders. But because you create a directory in PHP, you can only create one, what does it mean? Take a look at the following code to make it clear.
Code:
mkdir ("Cjlsoft/download/cpp_book", 0755);
?>
The code executes with an error because the folder hierarchy you want to create does not exist at all. Why do you say that the first directory "Cjlsoft" if it does not exist, the directory behind it is not created at all. If the directory "Cjlsoft" exists, the "download" directory does not exist. The "Cpp_book" you want to create won't even create a success! mkdir This function is the target created with the last directory. The "Cjlsoft/download/cpp_book" path mkdir function thinks you created the directory as "Cpp_book". If its parent does not exist, it is definitely not created.
The following warnings are available after the code executes:
Warning:mkdir (): No such file or directory in ...
So it doesn't work to create a directory! So to create such a hierarchical directory, we need to ensure that its parent directory exists. So I wrote a function that makes the directory easy to create successfully!
Function:
function Cjlsoft_mkdir ($path)
{
$dirs = Explode ("/", $path);
$current _dir = "";
foreach ($dirs as $dir)
{
$current _dir. = $dir. " /";
if (!file_exists ($current _dir))
{
@mkdir ($current _dir, 0755);
}
}
}
Cjlsoft_mkdir ("Cjlsoft/download/cpp_book");
This ensures that the hierarchy you want to create is available. function principle, the path you expect to separate, you know how many folders, there are how many layers. Thus a layer of one layer of combination. and create a directory.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.