Home > Backend Development > PHP Tutorial > PHP directory processing—browsing directories and operating directories

PHP directory processing—browsing directories and operating directories

黄舟
Release: 2023-03-07 13:46:02
Original
4732 people have browsed it

PHP Directory Processing—Browsing Directory and Operating Directory

In this article, we mainly talk about the browsing directory and operating directory of directory processing. Reading and writing operations like file processing, directory processing are similar to file processing, but the operation methods may be slightly different.

The previous article "PHP Directory Processing - Open/Close Directory" introduced the most basic opening and closing of directory processing. Below we will introduce in detail browsing the directory and operating the directory!

—: Browse the directory

To browse files in the directory in PHP, we use the scandir() function. The syntax format of this function is as follows:

array scandir ( string $directory [, int $sorting_order [, resource $context ]] )
Copy after login

This function returns an array containing all files and directories in directory. The parameter sorting_order specifies the sorting order. The default is ascending alphabetical order. If this parameter is added, it will be sorted in descending order.

The following example will view all files in a directory. The specific example code is as follows:

<?php
header("Content-Type:text/html; charset=utf-8");
$path = "D:\phpStudy\WWW\php";
if(is_dir($path)){                     //检测是否是一个目录
    $dir = scandir($path);             //使用 scandir()函数取得所有文件及目录
    foreach ($dir as $value){          //使用 foreach 循环
        echo $value."<br>";            //循环输出文件及目录名称
    }
}   else{
    echo "路径错误";
}
?>
Copy after login

The running results are as follows:

PHP directory processing—browsing directories and operating directories

2: Operating Directory

A directory is a special kind of file, that is to say, most of the operating functions for files (such as renaming) also apply to directories. But there are some special functions that are just for directories. The following list is some commonly used directory operation functions:

##void rewinddir(resource handle)Reassign the specified directory to the beginning of the directoryrewinddir($handle)
Function prototype Function description Example
bool mkdir(string $pathname) Create a specified directory mkdir('temp');
bool rmdir(string $dirname) Delete the specified directory, which must be empty rmdir('tmp')
string getcwd(void) Get the current working directory getcwd()
bool chdir(string directory) Change the current directory to directory echo getcwd()."
"; chdir('../');echo getcwd()."
"
float disk_free_space(string directory) Returns the free space (bytes) of the directory. The files being checked must be accessed through the server's file system disk_free_space('d:\\appserv');
float disk_total_space(string directory) Return the total space size of the directory (bytes) disk_total_sapce('d:\\appserv')
string readdir(resource handle) Return the file name of the next file in the directory (when using this function, the directory must be opened using the opendir() function). Before PHP5, this function was used to browse directories. while(false!==($path=readdir($handle))){echo $path;}
PHP directory processing ends here, as follows We continue to introduce PHP file processing. For details, please read "

Advanced Applications of PHP File Processing—Remote File Access and Locking Files"!

The above is the detailed content of PHP directory processing—browsing directories and operating directories. 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