This PHP tutorial help to understand PHP 7 file handling methods with examples.PHP has in-built file handling methods for common file operations.
A file is simply a resource for storing information on a computer.
Usually, files are used to hold information like:
PHP provides a set of in-built functions to handle files. Some of the functions are, fopen()
, file_exists()
, file_get_contents()
and etc.
The following list includes some of the fundamental file-related operations.
Here, We’ll disccuss here some PHP file handlimng methods like:
In PHP file handling, there are four sets of possible modes. These are,
r
and r+
} – To read the existing files.w
and w+
} – To change the entire file content.a
and a+
} – To add content after existing file content.x
and x+
} – To create a new file and work with it.This file_exists()
method helps to check whether the file is there in the directory before processing it. If the file you are looking for is not on the server, this PHP function also helps you in creating a new one.
file_exists($file_name);
Whereas Parameters:
$file_name
: variable is the file path.
It returns TRUE
if the file exists in the directory or FALSE
if the file doesn’t exist/found in the server/server directory.
The sample PHP code to check the file exists.
<?php If(file_exists('file_name.txt')) { echo "The File Found!"; } else{ echo "your file_name.txt doesnot exist!"; } ?>
This fopen()
method helps to open a file with the corresponding mode.
fopen($file_name, $mode, $use_include_path,$context);
Whereas Parameters:
The sample PHP code to open a file.
<?php $op = fopen("file_name.txt",'w'); or die("Failed in creating the file"); ?>
We have passed ‘w’ mode, It’ll perform the write operation only.
The entire file content will be cleared and the pointer will focus the start position of the file content. This method is used to change the existing file content, completely.
This write()
method helps to write contents into a file.
fwrite($handle,$string,$length);
Whereas Parameters:
$handle
: This is a file pointer’s.$string
: This is the data/information which is to be written inside the file..$length
: optional, This helps to specify the maximum file length.The sample php code to write a file.
<?php $op = fopen("hello.txt", "w"); fwrite($op, "PHPFlow: php tutorials\n"); ?>
First, clear the content of hello.txt and focus on the start position of the file content. This method is used to change the existing file content completely.
This fclose()
method help to close the file which is opened already in the server.
fclose($handle);
Whereas Parameters:
$handle
: This is a file pointer’s.The sample php code to close a file.
<?php $op = fopen("hello.txt", "w"); fwrite($op, "PHPFlow: php tutorials\n"); fclose($op); ?>
The above code writes the contents into the file then closed the file.
This fgets()
method help to help to read the file/files line by line.
fgets($handle);
Whereas Parameters:
$handle
: The resource of file pointer’s.The sample PHP code to read file line by line.
<?php $op = fopen("hello.txt",'r'); $lines = fgets($op); echo $lines; fclose($op); ?>
The above code helps to print all the contents of the hello.txt file then close the file.
This copy()
method helps to copy the files.
copy($file, $file_copied);
$file
: The source file path which is to be copied.$file_copied
: The name of the copied file.The sample PHP code to copy a file.
The above code will copy the file hello.txt
content into hello1.txt.
This file_get_contents()
method helps to n reading the entire contents of the file.
The main difference between the fgets()
and file_get_contents()
is that file_get_contents will return the whole data as a string but the fgets will be red the whole file line by line.
file_get_contents($file, $file_copied);
$file
: The source file path which is to be copied.$file_copied
: The name of the copied file.The sample PHP code to copy a file.
The unlink() method helps to delete the file from the target path.
unlink($file);
Whereas Parameters:
$file
: The source file path.The sample PHP code to delete a file.
<?php if(!unlink('hello1.txt')) { echo "Error! delete the file"; } else { echo "The file has been deleted successfully!"; } ?>
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More