Home Backend Development PHP Tutorial Basic file handling in PHP

Basic file handling in PHP

Jun 19, 2023 pm 03:45 PM
File operations php file processing File reading and writing

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:

<form method="post" enctype="multipart/form-data">
  <input type="file" name="file">
  <input type="submit" value="Upload">
</form>
<?php
$uploaded_file = $_FILES[&quot;file&quot;][&quot;tmp_name&quot;];
$destination = &quot;uploads/&quot; . $_FILES[&quot;file&quot;][&quot;name&quot;];
move_uploaded_file($uploaded_file, $destination);
?>
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(&quot;file.txt&quot;);
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 = &quot;uploads/&quot;;
$files = scandir($dir);
foreach($files as $file) {
  echo $file . &quot;&lt;br&gt;&quot;;
}
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(&quot;new_dir&quot;, 0777);
Copy after login
  1. rmdir()

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

Example:

rmdir(&quot;new_dir&quot;);
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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I delete gho files? Can I delete gho files? Feb 19, 2024 am 11:30 AM

Can I delete gho files?

Go Programming Tips: Deleting Contents from a File Go Programming Tips: Deleting Contents from a File Apr 04, 2024 am 10:06 AM

Go Programming Tips: Deleting Contents from a File

How to safely read and write files using Golang? How to safely read and write files using Golang? Jun 06, 2024 pm 05:14 PM

How to safely read and write files using Golang?

How to use pipes to read and write files in Golang? How to use pipes to read and write files in Golang? Jun 04, 2024 am 10:22 AM

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

How to fix: Java file operation error: File write failed How to fix: Java file operation error: File write failed Aug 26, 2023 pm 09:13 PM

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

How to optimize file reading and writing performance in C++ development How to optimize file reading and writing performance in C++ development Aug 21, 2023 pm 10:13 PM

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 Learn the file operation functions in Go language and implement the encryption, compression, upload and download functions of files Jul 29, 2023 pm 10:37 PM

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++? How to insert content at a specified location in a file using C++? Jun 04, 2024 pm 03:34 PM

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

See all articles