Updated March 31, 2023
Introduction to PHP file Functions
PHP file functions are the best and the convenient way of working with files we have with the help of PHP’s huge collection of the built-in functions. Windows Operating System and MAC Operating Systems are not the cases sensitive. Adopting to the lower case letters naming conversion for the file naming purpose is the best practice that ensures the maximum cross platform’s compatibility. There are some PHP File Functions that are very much helping in handling the data which is present in the file information.
PHP file Functions
PHP File Functions help in-store/delete/manipulate/copy the data in the file or deleting the file etc. Here is the list of some of the file functions. They are:
- file_exists Function
- fopen Function
- fwrite Function
- fclose Function
- fgets Function
- copy Function
- file_get_contents Function and
- deleting a File
Examples to Implement PHP file Functions
Below are the examples of PHP file Functions:
1. PHP file_exists Function
In order to write something in the file or manipulate the data in the delete however you want then at first, you have to check whether the file exists in the directory or not in order to process it. This PHP function also helps you in creating a new if the file you search is not present in the server and you want to create a new file at the server.
Syntax:
<?php
file_exists($file_name);
?>
Explanation:
“file_exists()” function is a PHP Function which returns the result as TRUE only if the file exists in the server or the result will FALSE if the file doesn’t exist/found in the server/server directory. $file_name variable is the file path and the name of the file at the end of the path which is to check.
Example:
This is the below example that uses the file_exists() function in order to determine the file whether it exists or not. Save the code below in the file_function.php which is in the syntax and open the file path in the browser so that you will see the result/output. File_name.txt is not created so the output will be the result of FALSE and the output of the ELSE condition statement will be the result.
Code:
<?php
If(file_exists('file_name.txt'))
{
echo "Now the File Found!!!";
}
else{
echo "your file_name.txt doesnot exist until now";
}
?>
Output:
2. PHP fopen Function
PHP fopen function will help you to open the file/files which are in the server.
Syntax:
<?php
fopen($file_name, $mode, $use_include_path,$context);
?>
Explanation:
- “fopen” is the PHP file function which is used to open the file which is in the server/server directory.
- “$file_name” is the actual file name which is to open
- “mode” is like what you want to do with the file like reading, writing, appending, etc.
- Mode “r” will read the file from the beginning and returns false if the file don’t even exist. It helps to read-only rather than read and write mode. For read and write mode, one must use “r+” mode.
- Mode “w” will helps to write some data to the file. It will truncate the file to the zero-length. If the file doesn’t even exist then the file will be created to write only rather than read and write. In order to read and write “w+” mode will be used.
- Mode “a” will appends the file at the end. If the file doesn’t even exist then the file will be created with write only mode. For read and write mode of appending then “a+” mode will be used.
- “$use_include_path” is the optional term and by default the result is false, if it is set to the TRUE result then the functions help the include path which is present too. Likewise “$context” is also optional which can be used in order to specify the context support.
Example:
The below syntax just open’s the file which has the name as file_name.txt and if not found then out will print which is in the die() function and die() function will be executed when the error occurs. Die() will display the message which is existed inside of the parenthesis. So no output in the browser mostly if the file really exists.
Code:
<?php
$op = fopen("file_name.txt",'w');
or
die("Now we are failed in creating the file");
?>
3. PHP write Function
PHP write function will help you to write files.
Syntax:
<?php
fwrite($handle,$string,$length);
?>
Explanation:
- “fwrite” PHP function will help to write some data to the files.
- “$handle” term is the file pointer’s resource.
- “$string” term is the data/information which is to be written inside the file.
- “$length” term is optional which helps to specify the maximum file length.
4. PHP Fclose Function
Fclose Function will help to close the file which is opened already in the server.
Syntax:
<?php
fclose($handle);
?>
Explanation:
- “fclose” will helps you to close the function which is opened already in the server/server directory.
- “$handle” is the pointer’s resource of the file.
5. PHP fgets Function
PHP Fgets Functions will help to read the file/files are red line by line using the syntax:
fgets($handle);
- “$fgets” is to read the lines of the file.
- “$handle” is the resource of the file pointer.
Code:
<?php
$op = fopen("file_name.txt",'r');
or
die("Now we are failed in opening the file");
$line1 = fgets(#op);
echo $line1;
fclose($op);
?>
6. PHP Copy Function
PHP copy function will be used in order to copy the files.
Syntax:
copy($file, $file_copied);
Explanation:
- “$file” is the path of the file which is to be copied.
- “$file_copied” term is the name of the copied file.
Code:
<?php
copy('file_name.txt','my_backup_settings.txt')
or
die("We can't cop the file");
echo "File now successfully copied to the 'my_backup_settings.txt'";
?>
7. PHP file_get_contents Function
This function helps in reading the entire contents of the file. Difference between the fgets and file_get_contents will return the whole data as a string but the fgets will be red the whole file line by line.
Code:
<?php
echo "<pre>"; // Enables the display of the line feeds
echo file_get_contents("file_name.txt");
echo "</pre>"; // Now it Terminates the pre tag
?>
8. Deleting a File (Unlink Function)
Unlink Function will help to delete a file.
Code:
<?php
if(!unlink('my_backup_settings.txt'))
{
echo " Cannot delete the file";
}
else
{
echo "file 'my_backup_settings.txt' now deleted successfully";
}
?>
All PHP File Functions help in supporting the wide range of some of the file formats. They are:
- File_name.txt
- File_name.log
- File_name.custom_extension i.e., file_name.xyz
- File_name.csv
- File_name.gif, File_name.jpg, etc.
- Files/File provides permanent cost-effective data storage solution/solutions for the simple data when compared to the databases which require some software and some skills in order to manage the Database Management Systems(DBMS Systems).
- File Functions helps to store some simple data like the server logs in order to analyze the data or for retrieving the data for future purpose.
- PHP file functions will help you to store the program/program settings which are like program.ini etc.
Recommended Article
This is a guide to PHP file Functions. Here we discuss the Introduction to PHP file Functions examples along with code implementation and output. You can also go through our other suggested articles to learn more –