You work as a web developer, you need to know the file system or file handling in PHP. The file is used to store the information and it can be Configuration, Log, images, Videos, CSV, etc. PHP has multiple functions for handling files.
In this article, we will learn the following topics:
- Create and Open a file
- Close a file
- Writing a file
- Reading a file
- Appending a file
- Mode description
- Checking file existence
- Deleting file a file
- Determining file size
- Rename a File
- Copy a File
Create or Open a file in PHP
We use
fopen()
function to create or open a file in PHP.
Syntax:
1 2 3 | <?php fopen(Filename, Mode); ?> |
Filename: It will be the name of the file which you have to create.
Mode: Mode can be “w” or “a”. All modes are described below.
Example:
1 2 3 4 | <?php $fileName = "test.txt"; $file = fopen($fileName, "w"); ?> |
In the above example, the file “test.txt” has been created and opened in write mode.
Close a file in PHP:
We use
fclose()
function to close a file in PHP.
Syntax:
1 | fclose(Handle); |
Handle: It will be the file that is open to write or read or append.
Write a file in PHP
We use
fwrite()
function to write a file in PHP. First, we will open the file in writing mode and write the content in that file.
Syntax:
1 | fwrite(Handle, String) |
Handle: It will be the file which we will write.
String: It will be the content to be written.
Example:
1 2 3 4 5 6 7 | <?php $fileName = "test.txt"; $file = fopen($fileName, "w"); $content = "This is text content."; fwrite($file, $content); fclose($file); ?> |
Read a file in PHP
The first parameter of
fopen()
function will be the file name and the second parameter will be in reading mode (“r”) and we use
fread()
function to read a file in PHP.
Syntax:
1 | fread(Handle, Length); |
Handle: It will be the file which we will read.
Length: We use
filesize()
function to calculate the length of the file.
Example:
1 2 3 4 5 6 7 | <?php $fileName = "test.txt"; $file = fopen($fileName, "r"); //Open file in read mode $fileContent = fread($file, filesize($fileName)); echo $fileContent; fclose($file); ?> |
Append a file in PHP
We open a file in append mode(“a”) to appending a file in PHP. The file does not exist then it creates a new one.
Example:
1 2 3 4 5 6 7 8 9 | <?php $fileName = "test.txt"; $file = fopen($fileName, "a"); //Open a file in append mode $content = "This is text content."; fwrite($file, $content); //Write content $moreContent = "Append content."; fwrite($file, $moreContent); //Append content fclose($file); ?> |
Mode description in PHP
Mode | Description |
---|---|
r | It is used to read the file only. Begins at the beginning of the file |
r+ | It is used to open a file in reading and writing mode. Begins at the start of the file |
w | It is used to open a file in write mode only. Opens and clears the file contents; or creates a new file when it doesn’t exist |
w+ | It is used to open a file in reading and writing mode. Opens and clears the file contents. It creates a new file when it doesn’t exist |
a | It is used to open a file in append mode. Opens and writes to the end of the file, or creates a new file if it is not available |
a+ | You can read and append file using this mode. It sets the file pointer at the file end. If the file does not exist then it creates. |
Check file existence in PHP
We use
file_exists()
function to check file existence in PHP.
Syntax:
1 | file_exists(filePath); |
1 2 3 4 5 6 7 8 9 10 11 12 | <?php $fileName = "test.txt"; /* You can use file name if code and file are in same directory otherwise you have to assign full path of the file to $fileName */ if(file_exists($fileName)) { echo "The File is available."; } else { echo "The File is not available."; } ?> |
Deleting a file in PHP
The
unlink()
function is used to delete a file in PHP.
Syntax:
1 | unlink(filename); |
Example:
1 2 3 4 5 | <?php $fileName = "test.txt"; unlink($fileName); echo "The File has been deleted successfully"; ?> |
In the above example, the file will be deleted if it will exist. Suppose the file does not exist then it will show a warning error message. In this case, you can check the file by condition statement.
Example:
1 2 3 4 5 6 7 8 9 | <?php $fileName = "test.txt"; if(file_exists($fileName)) { unlink($fileName); echo "The File has been deleted successfully."; } else { echo "The File is not available."; } ?> |
Determining file size in PHP
The
filesize()
function is used to determine file size in PHP. It returns file size in bytes.
Syntax:
1 | filesize(filename) |
Example:
1 2 3 4 | <?php $fileName = "test.txt"; echo filesize($fileName); ?> |
Rename file in PHP
We use
rename()
function to rename a file in PHP.
Syntax:
1 | rename(oldname, newname) |
Example:
1 2 3 4 5 | <?php $oldFileName = "test.txt"; $newFileName = "test-new.txt"; rename($oldFileName, $newFileName); ?> |
Copy file in PHP
We use
copy()
function to copy a file in PHP from one destination to another.
Syntax:
1 | copy(source, dest) |
Example:
1 2 3 4 5 | <?php $sourceFile = "test-new.txt"; $destinationFile = "test.txt"; copy($sourceFile, $destinationFile); ?> |