Home >Backend Development >PHP Tutorial >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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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:
Below are the examples of PHP file Functions:
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:
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:
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"); ?>
PHP write function will help you to write files.
Syntax:
<?php fwrite($handle,$string,$length); ?>
Explanation:
Fclose Function will help to close the file which is opened already in the server.
Syntax:
<?php fclose($handle); ?>
Explanation:
PHP Fgets Functions will help to read the file/files are red line by line using the syntax:
fgets($handle);
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); ?>
PHP copy function will be used in order to copy the files.
Syntax:
copy($file, $file_copied);
Explanation:
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'"; ?>
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 class="brush:php;toolbar:false">"; // Enables the display of the line feeds echo file_get_contents("file_name.txt"); echo ""; // Now it Terminates the pre tag ?>
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:
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 –
The above is the detailed content of PHP file Functions. For more information, please follow other related articles on the PHP Chinese website!