Basic file handling in PHP
Jun 19, 2023 pm 03:45 PMAs 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:
- fopen()
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");
- fread()
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;
- fwrite()
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()
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);
- $_FILES array
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:
<form method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="Upload"> </form> <?php $uploaded_file = $_FILES["file"]["tmp_name"]; $destination = "uploads/" . $_FILES["file"]["name"]; move_uploaded_file($uploaded_file, $destination); ?>
3. File deletion
- unlink()
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()
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>"; }
- mkdir()
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);
- rmdir()
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go Programming Tips: Deleting Contents from a File

How to safely read and write files using Golang?

How to use pipes to read and write files in Golang?

How to fix: Java file operation error: File write failed

How to optimize file reading and writing performance in C++ development

Learn the file operation functions in Go language and implement the encryption, compression, upload and download functions of files

How to insert content at a specified location in a file using C++?
