Home > Article > Backend Development > PHP folder and file related functions
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
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"
is_dir(filename);
filename: Specify the folder path to be determined.
Example:
is_dir("test_folder"); // Return true
opendir(path);
path: The path of the folder to be opened.
Example:
$dir = opendir("test_folder"); // Open the folder named "test_folder"
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
closedir(dir_handle);
dir_handle: The directory handle to be closed is obtained through the opendir() function.
Example:
closedir($dir); // Close the directory handle
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
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"
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
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
fclose(handle);
handle: The file handle to be closed, obtained through the fopen() function.
Example:
fclose($myfile); // Close the file handle
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!