PHP has strong file handling features that are necessary for developing websites. PHP provides a rich set of features and functions to reading from files, writing data to them or any type of file manipulation. In this article, we will learn about PHP file handling, and also explore its various functions with the help of examples.
PHP file handling use some set of functions to interact with files. We can do a lot of things to use these functions like reading, writing, appending, deleting files.
File is a sequence of data bytes, which is stored in a group. It is used to store any type of information and can change any time.
When we want to work with any file:
- Create: If it does not exists.
- Open: If it is already exists.
For use any option from above, PHP provides some modes to open / create a file. Modes are:
Modes | Meaning | Description |
r | Read only | Starts file reading from beginning |
r+ | Read/Write | Starts file reading or writing from beginning |
w | Write only | Start write to file from beginning, truncate file to length zero. If file does not exists, it creates |
w+ | Read/Write | Start read or write from beginning, truncate file to length zero. If file does not exists, it creates |
a | Append | Write from end of file. If file does not exists, it creates new |
a+ | Read/Append | Append from end of file. If file does not exists, it creates new |
We use some useful function related to file system in PHP. Functions are:
fopen
It is use to open a file.
fread
It is use to read a file.
fsize
It is use to calculate file size.
fgets
It is use to read a single line from file. File pointer moved to next line after use this function.
fwrite
It is use to write in a file.
fclose
It is use to close a file.
unlink
It is use to delete a file from folder.
fopen()
We use fopen function to open a file. Here, we use two parameters, one is file name and other is file modes.
Syntax
fopen(file_name, file_mode)
$file_name = 'abc.txt'; $open_action = fopen($file_name, 'w') or die('File does not exists');
fread()
We use fread funtion to read a file. Here, we use two parameters, one is file pointer and other is file size.
Syntax
fread(file_pointer, file_size)
$file_name = 'abc.txt'; $open_action = fopen($file_name, 'r') or die('File does not exists'); $read_data = fread($open_action, filesize($file_name)); echo $read_data;
fgets()
We use fgets function to read a file line by line. File pointer moves to next line after read a line.
Example
$file_name = 'abc.txt'; $open_action = fopen($file_name, 'r') or die('File does not exists'); while(!feof($open_action)) { echo fgets($open_action). ""; }
Here, we used feof in above example. It means, file pointer read file till End Of File(EOF).
Also read about Session & Cookies
fwrite()
We use fwrite to write content in a file.
Example
$file_name = 'abc.txt'; $open_action = fopen($file_name, 'w') or die('File does not exists'); $content = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry'; fwrite($open_action, $content);
fclose()
We use fclose to close a file.
Example
$file_name = 'abc.txt'; $open_action = fopen($file_name, 'w') or die('File does not exists'); fclose($open_action);
unlink()
We use unlink to delete an existing file.
Syntax
unlink(file_name);
unlink('abc.txt');
file_exists()
We use file_exists to check, file is exists on given path or not.
if(file_exists('abc.txt')) { echo "File does not exists"; } else { $fpointer = fopen('abc.txt', 'w+'); }
In above example, we are checking and create a new file, if file does not exists.
Building strong online applications, reading and writing data, and managing files are all made possible by PHP’s robust file handling features and best practices. For developers who want to construct feature-rich, dynamic online apps, mastering PHP file management is important.