In this tutorial, we will learn about PHP File Handling. File Handling is really useful if you want to perform some any file-related operations in your web application such as creating a new file, opening an existing file, delete a file etc. Here is a list of file-related operations:
Page Contents
- Creating a new file
- Opening an existing file
- Reading from file
- Writing in the file
- Closing a file
- Deleting a file
Creating a new File:
Many times we need to create a new file in our web application. We use fopen() function to create a new file in PHP File Handling. The fopen() function works in a very simple manner if a file already exists it will open that otherwise, it will create a new file. It is important to note if you want to create a new file then the file must file is opened for writing (w) or appending (a).
Example Program:
<?php
$newfile = fopen("NewFile.txt", "w") or die("Couldn't open this file");
fclose($newfile);
?>
Opening an existing file:
As we learned in the last example we use fopen() function to open a file. You can open a file to read to write or to append also. Please check out the following example.
Example Program:
<?php
$myfile = fopen("myFile.txt", "r") or die("Couldn't open this file");
fclose($myfile);
?>
Modes in which we can open a file:
Mode | Description |
---|---|
w | This mode is used to open a file just for writing in the file. If the file already not exist it will create a new file and if file exists it will remove all the existing data from the file. |
r | This mode is used to open a file just for reading from the file. |
a | This mode is used to open a file just for writing in the file. If the file already not exist it will create a new file and if file exists it will preserve all the existing data from the file and the file pointer will point out the end of the file. |
w+ | This mode is used to open a file for writing and reading the file. If the file already not exist it will create a new file and if file exists it will remove all the existing data from the file. |
r+ | This mode is used to open a file for reading and write the file |
a+ | This mode is used to open a file just for writing and reading the file. If the file already not exist it will create a new file and if file exists it will preserve all the existing data from the file and the file pointer will point out the end of the file. |
x | This mode is used to create a new file to write only. |
Reading from file:
We have fread() function in PHP to read a file. This fread() function can take two parameters first one is the name of the files and the second one is the number of bytes to read from the file.
Example Program:
<?php
$myfile = "c:\\myfile.txt";
$reading = fopen($myfile, "r");
$text = fread($reading, filesize($myfile));
echo $text;
fclose($handle);
?>
Writing in the file:
We have fwrite() function. We can create a new file using this function and write or append in the file. This fwrite() function takes to parameters first one is the filename and the second one is content which you want to write in the file.
<?php
$myfile = fopen("myfile.txt", 'w');
$content = "Hello, Welcome to Owlbuddy.\n";
fwrite($myfile, $content);
?>
Closing a file:
We have fclose() function in PHP to close opened file. This fclose() function takes an only a single parameter which is the file name.
<?php
$myfile = fopen("myfile.txt", 'w');
$content = "Hello, Welcome to Owlbuddy.\n";
fwrite($myfile, $content);
fclose($myfile);
?>
Deleting a file:
We have the unlink() function in PHP. To delete an existing file. This function takes only a single parameter which is the file name.
<?php
unlink('myfile.txt');
?>