Home > Backend Development > PHP Tutorial > Understand PHP's file operation functions

Understand PHP's file operation functions

WBOY
Release: 2023-05-11 17:14:02
Original
841 people have browsed it

PHP is a popular programming language that supports many file operation functions. In this article, we'll cover how to use these functions to read and write files, create and delete folders, and handle file uploads.

  1. Reading and writing files

The most basic file operation functions in PHP are fopen and fclose. The fopen function opens a file for reading and writing, while the fclose function closes the file.

$file = fopen("file.txt", "r");
// 读取整个文件
$content = fread($file, filesize("file.txt"));
echo $content;
fclose($file);

$file = fopen("output.txt", "w");
fwrite($file, "Hello World!");
fclose($file);
Copy after login

In the above example, we use the fopen function to open two files, one is a read-only file (file.txt) and the other is a writable file (output.txt). We read the entire file.txt file using the fread function and then closed the file using the fclose function. Next, we wrote a text "Hello World!" in the output.txt file, and then also used the fclose function to close the file.

In addition to the fread and fwrite functions, PHP also provides other file reading and writing functions, such as fgets, file_get_contents and file_put_contents, etc.

$file = fopen("file.txt", "r");
// 按行读取文件
while(!feof($file)) {
  echo fgets($file) . "<br>";
}
fclose($file);

$content = file_get_contents("file.txt");
echo $content;

$content = "Hello World!";
file_put_contents("output.txt", $content);
Copy after login
  1. Folder operation

In addition to file operations, PHP can also be used for folder operations. Use the mkdir function to create a folder, and use the rmdir function to delete a folder. In addition, PHP also provides other functions that can traverse files and subfolders in a folder, such as opendir, readdir, scandir, etc.

mkdir("my_folder");

if(is_dir("my_folder")) {
  rmdir("my_folder");
}

$dir = "my_folder";
if(is_dir($dir)) {
  if($dh = opendir($dir)) {
    while(($file = readdir($dh)) !== false) {
      echo "filename: " . $file . "<br>";
    }
    closedir($dh);
  }
}

$files = scandir($dir);
foreach($files as $file) {
  echo "filename: " . $file . "<br>";
}
Copy after login

In the above example, we first created a folder named my_folder and then deleted it using the rmdir function. Next, we use the opendir and readdir functions to loop through all the files and folders in the folder and output their names. Finally, we use the scandir function to get all the files and subfolders in the folder and iterate through them in a similar manner.

  1. File upload

In the process of website development, file upload is a common feature. PHP provides the $_FILES array to handle uploaded files. When processing uploaded files, we use the move_uploaded_file function to move the file from the temporary directory to its final location.

<form action="upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="file">
  <input type="submit" value="Upload">
</form>

<?php
if(isset($_FILES["file"])) {
  $file = $_FILES["file"];
  move_uploaded_file($file["tmp_name"], "uploads/" . $file["name"]);
}
?>
Copy after login

In the above example, we first added a file upload field to the HTML form. Then, when processing the uploaded file, we first check if the $_FILES array exists. If it exists, we use the move_uploaded_file function to move the file from the temporary directory to the uploads folder with the original file name.

Summary

In this article, we introduced how to use PHP file manipulation functions to read and write files, create and delete folders, and handle file uploads. These file manipulation functions are a must-have for any PHP developer. Mastering these functions requires practice and experimentation to better understand their purpose and functionality.

The above is the detailed content of Understand PHP's file operation functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template