PHP file Functions

王林
Release: 2024-08-29 13:02:25
Original
880 people have browsed it

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

  1. file_exists Function
  2. fopen Function
  3. fwrite Function
  4. fclose Function
  5. fgets Function
  6. copy Function
  7. file_get_contents Function and
  8. 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);
?>
Copy after login

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";
}
?>
Copy after login

Output:

PHP file Functions

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);
?>
Copy after login

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");
?>
Copy after login

3. PHP write Function

PHP write function will help you to write files.

Syntax:

<?php
fwrite($handle,$string,$length);
?>
Copy after login

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);
?>
Copy after login

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);
Copy after login
  • “$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);
?>
Copy after login

6. PHP Copy Function

PHP copy function will be used in order to copy the files.

Syntax:

copy($file, $file_copied);
Copy after login

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'";
?>
Copy after login

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 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 ?>
Copy after login

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";
}
?>
Copy after login

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 –

  1. Factorial in PHP
  2. PHP Pagination
  3. PHP unset()
  4. PHP MD5()

The above is the detailed content of PHP file Functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!