PHP file operation example directory operation

王林
Release: 2023-06-20 12:46:01
Original
1309 people have browsed it

In PHP, you can use a series of functions to operate on files and directories. This article will briefly introduce directory operations in PHP, including creating, deleting and traversing directories.

1. Create a directory

To create a directory in PHP, you can use the mkdir() function. The first parameter of this function is the path to the directory to be created, and the second optional parameter is the permission bits to be set. For example, the following code will create a directory named "test":

mkdir('test');
Copy after login

If you want to create a directory under the specified directory, you can add the path to the first parameter. For example, the following code will create a subdirectory named "subdir" in the directory named "test":

mkdir('test/subdir');
Copy after login

2. Delete the directory

For directories that are no longer used, you can Use the rmdir() function to delete it. The argument to this function is the path to the directory to be deleted. For example, the following code will delete a directory named "test":

rmdir('test');
Copy after login

Please note that the directory must be empty before using the rmdir() function to delete it. If the directory is not empty, a "Directory not empty" error will occur. If you want to force deletion of a non-empty directory, you can use the unlink() function to delete all files and subdirectories in the directory, and then use the rmdir() function to delete the directory itself. For example, the following code will delete the directory named "test" and all its contents:

function deleteDirectory($dir) {
    if (!file_exists($dir)) {
        return true;
    }

    if (!is_dir($dir)) {
        return unlink($dir);
    }

    foreach (scandir($dir) as $item) {
        if ($item == '.' || $item == '..') {
            continue;
        }

        if (!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
            return false;
        }
    }

    return rmdir($dir);
}

deleteDirectory('test');
Copy after login

3. Traverse the directory

To traverse all files and subdirectories in the directory, you can use the scandir() function . The argument to this function is the path to the directory to be scanned. For example, the following code will list all files and subdirectories in the directory named "test":

foreach (scandir('test') as $item) {
    if ($item == '.' || $item == '..') {
        continue;
    }

    echo $item . '<br>';
}
Copy after login

IV. Summary

This article introduces the three basic functions for directory operations in PHP :Create a directory (mkdir()), delete a directory (rmdir()), and traverse all files and subdirectories in a directory (scandir()). PHP's file and directory operation functions can help us create, delete and manage directories in web applications, making it more flexible and easier to maintain.

The above is the detailed content of PHP file operation example directory operation. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template