Home > Article > Backend Development > Getting Started with PHP: File Operations
As a beginner, PHP file operations may be one of the basic operations you need to master. File operations enable you to perform a variety of operations, including reading and writing files, creating and deleting files, and more importantly, processing uploaded files. In this article, we will explore the basics of PHP file operations.
Before performing file operations, you need to first check whether the file exists. This operation is very important because if you try to operate on a file that does not exist, you will encounter an error.
You can check whether the file exists through the file_exists() function. This function accepts a filename as a parameter and returns a Boolean value (true or false) to indicate whether the file exists.
For example, the following code can check whether the file "example.txt" exists:
if(file_exists("example.txt")){
echo "file exists";
} else {
echo "file does not exist";
}
Reading files is one of the most common behaviors in file operations. In PHP, you can easily read the entire contents of a file using the file_get_contents() function.
For example, the following code demonstrates how to read the file "example.txt":
$fileContent = file_get_contents("example.txt");
echo $fileContent;
You can also use the fopen() function to open the file and use the fgets() function to read each line of the file.
For example, the following code demonstrates how to use the fgets() function to read each line of the file "example.txt".
$file = fopen("example.txt", "r");
while(!feof($file)){
echo fgets($file). "<br>";
}
fclose($file );
If you need to write the contents to a file, you can use the file_put_contents() function. The function takes two parameters - one is the file name and the other is the content to be written. If the file does not exist, PHP will automatically create it.
For example, the following code demonstrates how to use the file_put_contents() function to write content to the file example.txt:
$fileContent = "This is a sample text.";
file_put_contents ("example.txt", $fileContent);
In PHP, you can create a new file using the touch() function. This function requires a filename as a parameter.
For example, in the following code, we are creating a new file named "newfile.txt":
touch("newfile.txt");
On the other hand, you can delete a file using the unlink() function. This function also requires a filename as a parameter.
For example, the following code demonstrates how to use the unlink() function to delete the file "newfile.txt":
unlink("newfile.txt");
In PHP, you can use the super global variable $_FILES to process uploaded files. When uploading a file, $_FILES stores some information about the uploaded file, such as file name, file size, and temporary file name. You can use it to move temporary files and save them on the server.
For example, the following code demonstrates how to move uploaded files to the server:
$targetDir = "uploads/";
$targetFile = $targetDir . basename($_FILES" fileToUpload");
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile)){
echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
The above is the basic knowledge of PHP file operations. Hopefully this guide will provide beginners with information about PHP file operations and help you learn PHP better.
The above is the detailed content of Getting Started with PHP: File Operations. For more information, please follow other related articles on the PHP Chinese website!