Hongmu Notes
Home Language Notes PHP: Check whether a directory with a specified characteristic exists; if not, create it.
Language Notes PHP

PHP: Check whether a directory with a specified characteristic exists; if not, create it.

PHP: Check whether a directory with a specified characteristic exists; if not, create it.

Write a PHP function that checks whether a specified directory contains a subdirectory with the prefix "admin-"; if such a directory exists, return its name; otherwise, create a new directory. The variable `admin-` represents the prefix, and the second argument is the name of the new directory to be created.

Here is a PHP function that implements this functionality. The function first checks whether there is a directory in the specified directory that starts with "admin-"; if such a directory exists, it returns its name; otherwise, it creates a new directory and returns the name of that new directory.

 隐藏内容:登录后可查看
function check_admin_dir($dir, $new_dir_name) {
    $handle = opendir($dir);
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && is_dir($dir.'/'.$file) && strpos($file, 'admin-') === 0) {
            closedir($handle);
            return $file;
        }
    }
    closedir($handle);
    $new_dir_path = $dir . '/' . $new_dir_name;
    if (!file_exists($new_dir_path)) {
        mkdir($new_dir_path, 0777, true);
        return $new_dir_name;
    }
    return false;
}

The first parameter of this function is the directory path to be checked; the second parameter is the name of the new directory to be created. Within the function, we use opendir()readdir() The function iterates through all files and subdirectories within a specified directory. If a directory with the prefix "admin-" is found, the function will utilize it. closedir() The function closes the directory handle and returns the directory name. If no directory with the prefix "admin-" is found, the function will use... mkdir() The function creates a new directory and returns the name of the new directory.

Here is an example of usage:

$dir = '/path/to/directory';
$admin_dir_name = 'admin-';
$new_dir_name = 'admin-JUSKD';

$result = check_admin_dir($dir, $new_dir_name);

if ($result === false) {
    echo 'Failed to create directory.';
} else if ($result === $new_dir_name) {
    echo 'Created new directory: ' . $result;
} else {
    echo 'Found existing directory: ' . $result;
}

In the above example, we first define the directory path to be checked and the name of the new directory to be created. Then, we call check_admin_dir() Call the function and pass these two parameters to it. Finally, check the result returned by the function and output the corresponding message based on that result. If the function returns falseIf the function returns `NULL`, it indicates that the directory creation failed; if the function returns a new directory name, it indicates that the new directory was successfully created; if the function returns the name of an existing directory, it indicates that an existing directory was found.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 141
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 189
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

(Adaptive Mobile Version) Responsive Marketing Technology – Personal Blog Website Template 0967

(Adaptive Mobile Version) Responsive Marketing Technology – Personal Blog Website Template 0967 Practical Collection pbootcms Template

A responsive marketing technology-themed personal blog template (PbootCMS), compatible with both PC and WAP devices. Its professional, tech-oriented design makes it ideal for technical professionals to share marketing strategies, SEO knowledge, and industry insights. This template helps tech bloggers build their personal brand and online influence. Template Preview | Installation Instructions | Website Admin Panel: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 32
Responsive EPE foam plastic website template 0741

Responsive EPE foam plastic website template 0741 Practical Collection Yiyou template

This EYouCMS responsive template is ideal for the EPE foam insulation industry, featuring a professional packaging design style that effectively showcases EPE foam products, packaging materials, technical specifications, and industrial applications. It enables packaging material companies to showcase their products online and attract industrial customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EYouCMS | EYouCMS...
👁 53
(PC + WAP) WeChat Mini Program Development Agency – Display and Sales; PBootCMS Website Template – Download Website Source Code from Software Development Company – 0046

(PC + WAP) WeChat Mini Program Development Agency – Display and Sales; PBootCMS Website Template – Download Website Source Code from Software Development Company – 0046 Practical Collection pbootcms Template

This PbootCMS template is designed specifically for brand showcasing by WeChat Mini Program development firms and software development companies, supporting both PC and WAP devices. Its modern, tech-oriented design makes it ideal for IT and internet technology enterprises. The template includes built-in modules for service offerings, development case studies, and technical team profiles, helping software companies efficiently acquire customers and build a strong industry reputation. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password:...
👁 63
(Adaptive Mobile Version) Responsive High-End Web Development Company Website Template – 0968

(Adaptive Mobile Version) Responsive High-End Web Development Company Website Template – 0968 Practical Collection pbootcms Template

A responsive, high-end website design template built with PbootCMS, compatible with both PC and WAP devices. Its innovative and sophisticated design makes it ideal for web design agencies to showcase their portfolio, services, and technical expertise. This template helps online businesses present a professional online image and attract corporate clients. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5....
👁 37
Second-hand goods recycling website template 0742

Second-hand goods recycling website template 0742 Practical Collection Yiyou template

An EYO CMS website template designed for the second-hand goods recycling industry. Its eco-friendly and practical design effectively showcases recycling services, end-of-life product handling, environmental sustainability initiatives, and the company's capabilities. This template helps renewable resource enterprises showcase their brand online and attract potential partners. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EYO CMS | EYO CMS (Eyo...
👁 44