Home > Backend Development > PHP Tutorial > Basic file handling in PHP

Basic file handling in PHP

WBOY
Release: 2023-06-19 15:52:01
Original
862 people have browsed it

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:

  1. 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");
Copy after login
  1. 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;
Copy after login
  1. 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);
Copy after login

2. File upload

  1. 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);
Copy after login
  1. $_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:

Copy after login

3. File deletion

  1. unlink()

The unlink() function is used to delete the specified file . This function accepts one parameter: file path.

Example:

unlink("file.txt");
Copy after login

4. File system operations

  1. 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>";
}
Copy after login
  1. 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);
Copy after login
  1. rmdir()

The rmdir() function is used to delete empty directories. This function accepts one parameter: directory path.

Example:

rmdir("new_dir");
Copy after login

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!

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