File and directory operations are the most basic functionality of a programming language. Just like the most common picture upload, file upload and other functions in our daily life, all need the support of file and directory operation. Today we’ll take a quick look at some of the PHP classes and functions for directory operations.

Directory and path separator

Let’s start with two constants that are delimiters for directories and paths.

echo DIRECTORY_SEPARATOR, PHP_EOL; // /
echo PATH_SEPARATOR, PHP_EOL; / / :
Copy the code

The DIRECTORY_SEPARATOR constant outputs a slash like/on Linux and a backslash like \ on Win. That is, it depends on the current operating system to determine which directory separator to use. PATH_SEPARATOR is the environment PATH separator. In Linux, the output is:. In fact, just like the system environment variable we define, in Linux, we view or add or modify the PATH environment variable is separated by:.

Directory object operations

Since there is the concept of object, then also shows that directory related operations are actually object-oriented and procedural two forms, let’s take a look at how object-oriented operations.

$directory = dir('.. / ');

while(($dir = $directory->read())! = =false) {echo $dir, PHP_EOL;
}
// .
// ..
// 1.php date-related function (c).md
// source
Copy the code

We use the dir() function to return a Directory object. It is important to note that Directory objects can only be retrieved by the dir() function, and cannot be generated by new. After obtaining the Directory object, we can read all the contents of the Directory through the read() method. The display is similar to the result obtained by using the ll -a command on the operating system. The two directories represent the current directory and the upper directory.

echo $directory->read(), PHP_EOL;
//

$directory->rewind();
while(($dir = $directory->read())! = =false) {echo $dir, PHP_EOL;
}
// .
// ..
// 1.php date-related function (c).md
// source
Copy the code

The read() method is a cursor operation, that is, the cursor moves to the last position after traversing it once, at which point a call to read() will have no information. At this point we can reset the cursor with the rewind() method, and then iterate over the contents of the directory.

$directory->close();
// while($dir = $directory->read()){
// echo $dir, PHP_EOL;
// }
// Warning: Directory::read(): supplied resource is not a valid Directory resource 
Copy the code

Finally, the close() method closes the current directory object handle.

Procedure-oriented Directory operations

Now let’s look at how to retrieve the contents of the directory using the procedural approach. We’ll look at the same directory and output the same information as in the object-oriented approach above.

$dirPath = __DIR__;
if(is_dir($dirPath)) {if ($dh = opendir($dirPath)) {
        while(($dir = readdir($dh))! = =false) {echo $dir, PHP_EOL;
        }
        echo readdir($dh), PHP_EOL;
        

        rewinddir($dh);
        while(($dir = readdir($dh))! = =false) {echo $dir, PHP_EOL;
        }

        closedir($dh); }}// .
// ..
// 2. Learn about directory operations in PHP
// date-related functions in 1.php (3).php
//
// .
// ..
// 2. Learn about directory operations in PHP
// date-related functions in 1.php (3).php
Copy the code

The is_dir() function is used to determine whether a given path is a directory. Opendir () is a Directory handle to open a given path. It is similar to the dir() function to retrieve a Directory object, but it is important to note that opendir() returns a handle resource object, not a Directory object. We can then use the readdir() function to read all the directory information content in the handle object.

Like the read() method on Directory objects, readdir() operates on cursors, and when traversed, we can reset the handle cursor with the rewinddir() function. Finally, there is the closedir() function, which also closes a handle resource.

Other directory path operations

In addition to walking through directories, there are other functions to help us get information about directories.

echo getcwd(), PHP_EOL;
// /Users/ Zhangyue /MyDoc/ blogpost

chdir('dev-blog/php/202010');

echo getcwd(), PHP_EOL;
// /Users/zhangyue/MyDoc/ blog-blog/php.202010

// chdir('dev-blog/php/202013'); 
// Warning: chdir(): No such file or directory (errno 2) 

chdir('/home');
echo getcwd(), PHP_EOL;
// /System/Volumes/Data/home
Copy the code

The getcwd() function gets information about the directory where the PHP script is currently running. Note that it is the path to the PHP command script from where you are running it, the working directory of the PHP program, not the path to the file you are currently running. You can change this directory information using chdir(). If the concepts of the getcwd() function are not particularly clear, be sure to test them out manually.

print_r(scandir($dirPath));
// Array
/ / (
/ / [0] = >.
/ / [1] = >..
// [2] => 1.php date-related function (三).php
// [3] => 2
// )

print_r(scandir('/Users'));
// Array
/ / (
/ / [0] = >.
/ / [1] = >..
// [2] => .localized
// [3] => Guest
// [4] => Shared
// [5] => share
// [6] => zhangyue
// )
Copy the code

The scandir() function lists files and directories in a specified path. Its argument must be an absolute path that returns everything as an array, which is handy in some cases.

conclusion

That’s all there is to know about directories in PHP. There are also functions like mkdir() that operate directories, but it’s covered in the PHP documentation for file manipulation, so we’ll cover that later when we learn about file manipulation.

Test code:

Github.com/zhangyue050…

Reference Documents:

www.php.net/manual/zh/b…

Follow the public account: [Hardcore project manager] to get the latest articles

Add WeChat/QQ friends: free xiaoyuezigonggong / 149844827 】 【 PHP, project management, learning materials

Zhihu, Public Account, Douyin, Toutiao Search [Hardcore Project Manager]

ID of station B: 482780532