File Handling in PHP with Examples [2 Steps]

In this article we are going to cover File Handling in PHP with Examples, What is File Handling in PHP, Overwriting File in PHP.

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
File Handling in PHP with Examples [2 Steps] 1
fopen

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
File Handling in PHP with Examples [2 Steps] 2
fgets

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
File Handling in PHP with Examples [2 Steps] 3
feof

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
File Handling in PHP with Examples [2 Steps] 4
fwrite

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
File Handling in PHP with Examples [2 Steps] 5
overwritting

Conclusion:

We have covered File Handling in PHP with Examples

Related Articles:

Reference:

File Handling in PHP official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap