In this article we are going to cover File Handling in PHP with Examples, What is File Handling in PHP, Overwriting File in PHP.
Table of Contents
What is File Handling in PHP?
In this PHP web-programming language we will take a look at file handling. We will take a look at how to open en close a file. How to read a file line by line and how to read a file character by character.
Modes Description:
r – File is opened for read only.
w- Opens a file for write only. If file not exist then new file is created and if file already exists then contents of file is erased.
a- File is opened for write only. File pointer points to end to file. Existing data in file is preserved.
w+ – Opens file for read and write. If file not exist then new file is created and if file already exists then contents of file is erased.
r+- File is opened for read/write.
a+ – File is opened for write/read. File pointer points to end of file. Existing data in file is preserved. If file is not there then new file is created.
x- New file is created for write only.
PHP Open File – fopen()
The PHP fopen() function is used to open a file. First parameter of fopen() contains name of the file which is to be opened and second parameter tells about mode in which file need to be opened.
Example of fopen() in PHP:
$file = fopen("file_handling.txt", "r")
PHP read file – fread()
The PHP fread() function is used to reads from an open file. First parameter of fread() contains name of the file which is to be read and second parameter specifies the maximum number of bytes to read.
Example of fread() in PHP:
fread($file,filesize("file_handling.txt"));
PHP Close File – fclose():
The fclose() function is used to close an open file. Its argument is file which need to be closed.
Example of fclose() in PHP:
<?php
$file = fopen("file_handling.txt", "r");
// some code to be executed....
fclose($file);
?>
Example of open, read and close file in PHP:
<!DOCTYPE html>
<html>
<body>
<?php
$file = fopen("file_handling.txt", "r") or die("Unable to open file!");
echo fread($file,filesize("file_handling.txt"));
fclose($file);
?>
</body>
</html>
Output:
AJAX = Asynchronous JavaScript and XML CSS = Cascading Style Sheets HTML = Hyper Text Markup Language PHP = PHP Hypertext Preprocessor SQL = Structured Query Language SVG = Scalable Vector Graphics XML = EXtensible Markup Language
PHP Read Single Line – fgets()
The getc() function is used to read a single character from a file.
Example of read single line using fgets() function in PHP:
<!DOCTYPE html>
<html>
<body>
<?php
$file = fopen("file_handling.txt", "r") or die("Unable to open file!");
echo fgets($file);
fclose($file);
?>
</body>
</html>
Output:
AJAX = Asynchronous JavaScript and XML
PHP Check End-Of-File –feof()
The feof() function checks if the “end-of-file” has been reached. This function is useful for looping through data of unknown length.
Example of check end-of-file in PHP:
<!DOCTYPE html>
<html>
<body>
<?php
$file = fopen("file_handling.txt", "r") or die("Unable to open file!");
while(!feof($file)) {
echo fgets($file) . "<br>";
}
fclose($file);
?>
</body>
</html>
Output:
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
PHP Write to File – fwrite()
The fwrite() function is used to write to a file.
Example of write to file in PHP:
<!DOCTYPE html>
<html>
<body>
<?php
$file = fopen('data.txt', 'w');//opens file in write-only mode
fwrite($file, 'welcome ');
fwrite($file, 'to php file write');
fclose($file);
echo "File written successfully";
?>
</body>
</html>
Output:
File written successfully
Overwriting File in PHP:
If you run the above code again, it will erase the previous data of the file and write the new data.
Example of overwriting file in PHP:
<!DOCTYPE html>
<html>
<body>
<?php
$file = fopen('data.txt', 'w');
fwrite($file, 'Hello');
fclose($file);
echo "File overwriting";
?>
</body>
</html>
Output:
File overwriting
Conclusion:
We have covered File Handling in PHP with Examples
Related Articles:
- Introduction about PHP [PHP 7]
- Date and Time in PHP with Examples [2 Steps]
- 3 Data Types in PHP with Examples
- Form Validation in PHP with Examples
- 3 Types of Arrays in PHP with Examples
- 4 Types of Loop in PHP with Examples
- 4 Types of String in PHP with Examples
- Cookies in PHP with Examples [2 Steps]
- File Upload in PHP with Examples
Reference: