Home  >  Article  >  Backend Development  >  PHP folder and file related functions

PHP folder and file related functions

王林
王林Original
2023-06-16 11:25:391628browse

PHP is a programming language used for website development and is often used to handle file and folder operations. This article will introduce commonly used folder and file related functions in PHP.

1. Folder related functions

  1. mkdir() function: This function is used to create a new folder. The syntax is:

mkdir(filename, mode, recursive, context);

filename: Specify the path of the folder to be created.

mode: This parameter determines the permissions of the created folder. The default is 0777.

recursive: can be a Boolean value. If the value is true, multi-level directories can be created recursively (the default is false, that is, no recursion).

context: Specifies the context of the folder. Generally, there is no need to set it.

Example:

mkdir("test_folder"); // Create a folder named "test_folder"
mkdir("parent_folder/child_folder", 0777, true); / / Recursively create a folder named "child_folder"

  1. is_dir() function: This function is used to determine whether a path is a folder and returns a Boolean value. The syntax is:

is_dir(filename);

filename: Specify the folder path to be determined.

Example:

is_dir("test_folder"); // Return true

  1. opendir() function: This function is used to open a folder and return a Directory handle (also called a stream pointing to a directory). The syntax is:

opendir(path);

path: The path of the folder to be opened.

Example:

$dir = opendir("test_folder"); // Open the folder named "test_folder"

  1. readdir() function: Function is used to read the file name in an open folder. Each call to this function reads the next file name in the folder, returning the string value of that file name (returns false if there are no more files). The syntax is:

readdir(dir_handle);

dir_handle: The directory handle to be read is obtained through the opendir() function.

Example:

$dir = opendir("test_folder");
while(false !== ($file = readdir($dir))){
echo $ file . " ";
}
closedir($dir); // Output the file name in the folder and close the directory handle

  1. closedir() function: This function is used Close an open directory handle to ensure resource release. The syntax is:

closedir(dir_handle);

dir_handle: The directory handle to be closed is obtained through the opendir() function.

Example:

closedir($dir); // Close the directory handle

  1. rmdir() function: This function is used to delete a folder. The syntax is:

rmdir(dirname);

dirname: The path of the folder to be deleted.

Example:

rmdir("test_folder"); // Delete the folder named "test_folder"

2. File-related functions

  1. fopen() function: This function is used to open a file and return a file handle (also called a stream pointing to the file) for reading, writing and closing the file. The syntax is:

fopen(filename, mode, use_include_path, context);

filename: the file name or path to be opened.

mode: This parameter determines the operations that can be performed after the file is opened. The default is "r" (read-only).

use_include_path: can be a Boolean value. If the value is true, include_path is also used when searching for files (the default is false, that is, not used).

context: Specifies the file context, and generally does not need to be set.

Example:

$myfile = fopen("test.txt", "r"); // Open the file named "test.txt"

  1. fread() function: This function is used to read data from an open file and return the read data string. The syntax is:

fread(handle, length);

handle: The file handle to read the data, obtained through the fopen() function.

length: The number of bytes to read.

Example:

$myfile = fopen("test.txt", "r");
echo fread($myfile, filesize("test.txt"));
fclose($myfile); // Output the data in the file and close the file handle

  1. fwrite() function: This function is used to write data to an open file. The syntax is:

fwrite(handle, string, length);

handle: The file handle to which data is to be written, obtained through the fopen() function.

string: The string to be written.

length: The number of bytes to be written. If omitted, the entire string is written.

Example:

$myfile = fopen("test.txt", "w");
$txt = "Hello world!";
fwrite($myfile, $txt);
fclose($myfile); //Write "Hello world!" to the file and close the file handle

  1. fclose() function: This function is used to close an existing Open file handle to ensure resource release. The syntax is:

fclose(handle);

handle: The file handle to be closed, obtained through the fopen() function.

Example:

fclose($myfile); // Close the file handle

  1. unlink() function: This function is used to delete a file. The syntax is:

unlink(filename);

filename: the file name or path to be deleted.

Example:

unlink("test.txt"); // Delete the file named "test.txt"

3. Summary

This article introduces several common functions related to folders and files in PHP. Through these functions, we can easily create folders, read and write files, close file handles, and other operations. In practical applications, these functions can save us a lot of time and effort.

The above is the detailed content of PHP folder and file related functions. 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