Hongmu Notes
Home Language Notes Check whether there is a directory with the prefix "admin-".
Language Notes PHP

Check whether there is a directory with the prefix "admin-".

Check whether there is a directory with the prefix "admin-".

To check whether there is a directory with the prefix "admin-", you can use PHP's file system functions and string functions to accomplish this. The specific steps are as follows:

  1. Use opendir() The `OpenDirectory` function opens the current directory and returns a directory handle.
  2. Use readdir() The function reads files and subdirectories from a specified directory and uses them. substr() The function retrieves the prefix of the directory name.
  3. If the directory name starts with "admin-", it indicates that a directory with the prefix "admin-" exists. This can be used. closedir() The function closes the directory handle and returns `true`; otherwise, it continues to traverse the files and subdirectories within the directory.
  4. If no directory with the prefix "admin-" is found after traversing the entire directory structure, you can use this method. closedir() The function closes the directory handle and returns `false`.

Here is an example code snippet:

 隐藏内容:会员可查看
function has_admin_dir($dir) {
    if (is_dir($dir)) {
        $handle = opendir($dir);
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                if (substr($file, 0, 6) == "admin-") {
                    closedir($handle);
                    return true;
                } else if (is_dir($dir.'/'.$file)) {
                    if (has_admin_dir($dir.'/'.$file)) {
                        closedir($handle);
                        return true;
                    }
                }
            }
        }
        closedir($handle);
        return false;
    }
}

In this example, we use a recursive function. has_admin_dir() Iterates through all files and subdirectories within a specified directory. If a directory with the prefix "admin-" is found, the function returns `true`; otherwise, it returns `false`. To call this function, simply pass the path of the directory you wish to check. For example:

if (has_admin_dir('/path/to/your/directory')) {
    echo "Found admin directory.";
} else {
    echo "Admin directory not found.";
}

Note: This example code is only intended for checking whether any directory with the prefix "admin-" exists in the current directory or its subdirectories. If you want to check whether any directory with the prefix "admin-" exists in the direct subdirectories of a specified directory, you can remove the recursive code section and use it within the loop. is_dir() The function determines whether the current file is a directory.

微信赞赏

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']=…
👁 190
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

(PC+WAP) Emotional news and information website pbootcms template Emotional information website source code download 0047

(PC+WAP) Emotional news and information website pbootcms template Emotional information website source code download 0047 Practical Collection pbootcms Template

An emotional and psychological information PbootCMS website template that supports both PC and WAP access. The design style is soft and warm, which is very suitable for publishing emotional articles, mental health and life insights. The page layout is clear and the reading experience is comfortable. It is an ideal choice for building an emotional blog or psychological information website. Template display Installation instructions Website backend:/admin.php Account: admin Password: admin…
👁 47
Water purification equipment water treatment enterprise website template 0744

Water purification equipment water treatment enterprise website template 0744 Practical Collection Yiyou template

A eyoucms website template for water purification equipment and water treatment enterprises. Design style of science and technology environmental protection, can display water purification products, water treatment system, technical solutions and engineering cases. Help water treatment companies display their brands online and attract home and industrial customers. Template display, installation instructions website background: / login.php account: admin password: admin related article easy you CMS installation FAQ summary easy you...
👁 61
(Adaptive Mobile Version) Responsive High-End Bathroom Product Website Template – 0970

(Adaptive Mobile Version) Responsive High-End Bathroom Product Website Template – 0970 Practical Collection pbootcms Template

A responsive, high-end bathroom product website template based on PbootCMS, compatible with both PC and WAP devices. Featuring a modern, luxurious design style, it is ideal for showcasing premium bathroom products, spatial designs, and brand identities. This template helps bathroom brands attract designers and high-end customers online. Template Preview | Installation Instructions | Website Admin Panel: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles: Pb...
👁 50
(PC + WAP) Adult Education Examination Self-Study Page Landing Page – PBootCMS Website Template; Download Source Code for Adult Education and Training Single-Page Websites – 0048

(PC + WAP) Adult Education Examination Self-Study Page Landing Page – PBootCMS Website Template; Download Source Code for Adult Education and Training Single-Page Websites – 0048 Practical Collection pbootcms Template

This is a PbootCMS landing page template designed for adult education examinations and self-directed learning programs, compatible with both PC and mobile devices. The design focuses on driving marketing conversions; the page features a compact structure that highlights course advantages, faculty expertise, and the enrollment portal. It is ideal for educational institutions using Baidu PPC advertising or SEM campaigns to effectively increase the enrollment conversion rate for adult education courses. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin...
👁 36
Environmental-friendly plastic and paper cup website templates – 0745

Environmental-friendly plastic and paper cup website templates – 0745 Practical Collection Yiyou template

This EyouCMS template is ideal for businesses specializing in eco-friendly plastic or paper water bottles and cup products. Its eco-conscious and practical design effectively showcases your water bottle collections, paper cup series, sustainable materials, and brand identity. It helps environmental goods companies showcase their products online and attract both wholesale and retail customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS...
👁 67
(Adaptive Mobile Version) Greenery Potted Plant Website Template – Potted Plant Rental Website Source Code Download: 0971

(Adaptive Mobile Version) Greenery Potted Plant Website Template – Potted Plant Rental Website Source Code Download: 0971 Practical Collection pbootcms Template

A PbootCMS website template for greenery potted plants and potted plant rental services, compatible with both PC and WAP devices. Its fresh and natural design is ideal for showcasing greenery products, potted plant designs, and rental services. This template helps gardening companies attract both corporate and individual clients online, thereby expanding their business. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles...
👁 33