As a popular server-side programming language, PHP (Hypertext Preprocessor) can not only be used to process Web requests, but also read and write files in local and remote file systems. In this article, we will introduce the most commonly used file processing functions and techniques in PHP, including file reading and writing, file uploading, file deletion, file system operations, etc.
1. File reading and writing
In PHP, we read and write files through the following functions:
The fopen() function is used to open a file and returns a file handle. This handle is used for subsequent read and write operations. This function accepts two parameters: file name and opening mode. The open mode specifies the way to operate the file, for example: "r" means read-only, "w" means write-only, "a" means readable and writable, etc.
Example:
$file = fopen("file.txt", "r");
The fread() function is used to read the specified number of bytes from an open file. This function accepts two parameters: the file handle and the number of bytes to read. The read result will be returned as a string.
Example:
$file = fopen("file.txt", "r"); $content = fread($file, filesize("file.txt")); fclose($file); echo $content;
The fwrite() function is used to write the specified string to a file. This function accepts two parameters: the file handle and the string to be written.
Example:
$file = fopen("file.txt", "w"); fwrite($file, "Hello World!"); fclose($file);
2. File upload
move_uploaded_file() function is used to upload the file Move to the specified directory. This function accepts two parameters: the temporary path of the uploaded file and the target path.
Example:
$uploaded_file = $_FILES["file"]["tmp_name"]; $destination = "uploads/" . $_FILES["file"]["name"]; move_uploaded_file($uploaded_file, $destination);
In PHP, $_FILES is a special array used to handle files uploaded via HTTP POST. This array contains information such as the attributes and temporary path of the uploaded file.
Example:
3. File deletion
The unlink() function is used to delete the specified file . This function accepts one parameter: file path.
Example:
unlink("file.txt");
4. File system operations
scandir() function is used to list the specified The names of all files and directories in the directory. This function accepts one parameter: directory path.
Example:
$dir = "uploads/"; $files = scandir($dir); foreach($files as $file) { echo $file . "<br>"; }
The mkdir() function is used to create a new directory. This function accepts two parameters: directory path and permission settings.
Example:
mkdir("new_dir", 0777);
The rmdir() function is used to delete empty directories. This function accepts one parameter: directory path.
Example:
rmdir("new_dir");
Summary
In PHP, we can easily read, write, upload, delete, and operate the file system on files. Mastering these basic file processing skills will help us write server-side applications more efficiently. At the same time, we should also pay attention to the security of the file system to avoid security risks caused by the upload and execution of malicious files.
The above is the detailed content of Basic file handling in PHP. For more information, please follow other related articles on the PHP Chinese website!