Home  >  Article  >  Backend Development  >  How to use file and directory handling functions in PHP?

How to use file and directory handling functions in PHP?

WBOY
WBOYOriginal
2023-07-25 09:58:55637browse

How to use file and directory processing functions in PHP?

In web development, working with files and directories is a common task. PHP provides a series of file and directory processing functions, allowing developers to easily operate files and directories.

First, let's look at how to create, delete and write files.

  1. Create File
    Use PHP's file_put_contents function to write content to a file and create a new file. The following is a sample code:
$file = 'test.txt';
$content = 'Hello World!';

// 将内容写入文件
file_put_contents($file, $content);

The above code will create a file named test.txt in the current directory and write the content 'Hello World!' into it.

  1. Delete files
    Use PHP's unlink function to delete the specified file. The following is a sample code:
$file = 'test.txt';

// 删除文件
unlink($file);

The above code will delete the file named test.txt in the current directory.

  1. Write File
    Use PHP's file_put_contents function to write content to an existing file. The following is a sample code:
$file = 'test.txt';
$content = 'Hello World Again!';

// 将内容写入已存在的文件
file_put_contents($file, $content);

The above code will write the content 'Hello World Again!' to the existing test.txt file.

Next, let’s take a look at how to create, delete and traverse directories.

  1. Create Directory
    Use PHP's mkdir function to create a new directory. The following is a sample code:
$dir = 'test';

// 创建目录
mkdir($dir);

The above code will create a directory named test in the current directory.

  1. Delete directory
    Use PHP's rmdir function to delete the specified directory. The following is a sample code:
$dir = 'test';

// 删除目录
rmdir($dir);

The above code will delete the directory named test in the current directory.

  1. Traverse the directory
    Use PHP's scandir function to obtain the list of files and subdirectories in the specified directory. The following is a sample code:
$dir = 'test';

// 获取目录列表
$files = scandir($dir);

foreach($files as $file){
    echo $file . "<br>";
}

The above code will traverse the test directory under the current directory and print out the list of files and subdirectories in it.

To sum up, PHP provides a wealth of file and directory processing functions, allowing developers to easily create, delete and traverse files and directories. By mastering the use of these functions, developers can more efficiently complete file and directory processing tasks in web development.

The above is the detailed content of How to use file and directory handling functions in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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