PHP function introduction—mkdir(): Create directory
Introduction:
In web development, sometimes we need to dynamically create directories on the server to store files uploaded by users, temporary files or other data. PHP's mkdir() function is designed to facilitate us to create directories on the server. This article will introduce the usage and code examples of the mkdir() function.
1. Overview of mkdir() function:
The mkdir() function is used to create a directory on the specified path. Its parameters are as follows:
2. mkdir() function example:
The following are several example codes for using the mkdir() function to create a directory:
Simple directory Create:
<?php $dir = 'path/to/new/directory'; if (!is_dir($dir)) { mkdir($dir); echo '目录创建成功!'; } else { echo '目录已存在!'; } ?>
Set directory permissions:
<?php $dir = 'path/to/new/directory'; $mode = 0755; if (!is_dir($dir)) { mkdir($dir, $mode); echo '目录创建成功!'; } else { echo '目录已存在!'; } ?>
Create directory recursively:
<?php $dir = 'path/to/new/recursive/directory'; if (!is_dir($dir)) { mkdir($dir, 0777, true); echo '目录创建成功!'; } else { echo '目录已存在!'; } ?>
Dynamic Create a directory:
<?php $dir = 'path/to/new/' . date("Y/m/d/"); if (!is_dir($dir)) { mkdir($dir, 0777, true); echo '目录创建成功!'; } else { echo '目录已存在!'; } ?>
In the above example code, we have implemented functions such as simple directory creation, setting directory permissions, recursively creating directories, and dynamically creating directories. In actual applications, you can choose a suitable directory creation method according to your own needs.
Summary:
Through the introduction of this article, we have learned about the usage of the mkdir() function in PHP and common sample codes. Use the mkdir() function to easily create a directory on the server and set permissions for it. In actual development, we can flexibly use the mkdir() function to meet our directory creation needs based on specific needs and permission settings of the code running environment. Hope this article helps you!
The above is the detailed content of Introduction to PHP functions—mkdir(): Create directory. For more information, please follow other related articles on the PHP Chinese website!