File Handling in PHP | File Management in PHP With All Operations

File Handling in PHP

File handling in PHP is an important part of any application. you need any application to open and process a file for different tasks. PHP (Hypertext Preprocessor) has many functions to work with normal files. the file handling in PHP is similar work as file handling using any programming language like C (file handling in C).

The PHP file system allows us to create files, read file character by character, read file line by line, append file, write a file, delete a file and close file. The file system Functions allow you to access and manipulate (PHP file Manager) the file. the file system provides a concept to start specific data using different types of File format.

File Formats Support in PHP

PHP Files functions support a wide range of files formats which are given as follows

  • File.log
  • File.txt
  • File.csv
  • File.gif
  • File.jpg
  • File.custom_extension such as file.xyz
  • the files provide a permanent cost-effective data storage solution for simple data compared to the database that requires skills to manage DBMS (Data Base Management Systems)systems and other software.
  • You want to store normale data or simple data such as server logs for later analysis and retrieval.
  • you want to store program settings such as file extension (program.ini)

PHP Files Manipulating | PHP Files Several Functions

Therefore, PHP (Hypertext Preprocessor) has few functions for reading, uploading, creating, and editing files. PHP provides a superior way of working with files via its rich collection of built-in functions.

Operating systems (OS) such as MAC OS and Windows are not case sensitive while UNIX or Linux operating systems are case sensitive.

Adopting a naming conversion like lower case letters only for file naming is a better practice that ensures maximum cross-platform compatibility.

PHP File_exists Function

In PHP (Hypertext Preprocessor) File_exists function are used to determine whether a file exists or not.

this function comes when we want to know that the file exists or not before processing it.

with the help of this function, you can create a new file and used this function when you want to ensure about the file does not already exist on the server.

The PHP file_exit function has the general syntax.

Explanation:
“file_exists()” checked the file exists or not, if the file exists its return true otherwise its return always false if the file does not exist.
“$file_name” is the name of the file and the path to be checked.
the following given below code uses file_exists function to determine if the file file_settings.txt exists.

the following code save in a file named file_functions.php Assuming that you saved this file in “phptest” folder in htdocs, after that open the URL http://localhost/phptest/file_functions.php in your web browser, you will get the results.

PHP Fopen Function

there are the following fopen function is used to open files, the general syntax are:-

EXplanation: fopen in PHP function general syntax

  • “fopen” PHP function is used to open the file.
  • “$file_name” is the name of the file to be opened (opened file name).
  • “$mode” is the mode in which the file should be opened, the following given table below shows the modes.
Mode Description
w
  • Open a file for write only
  • if the file does not exist then a new file is created and
  • if the file already exists then the contents of the file are erased.
w+
  • Open a file for reading and write
  • if the file does not exist then a new file is created and
  • if the file already exists then the contents of the file are erased.
r
  • the file is opened for read-only. (Read the file from the beginning returns false if the file doesn’t exist then read-only)
r+
  • The file is opened for reading/write. (Read the file from the beginning returns false if the file doesn’t exist then read and write only)
a
  • The file is opened for write-only.
  • File pointer points to the end of the file.
  • Existing data in the file is preserved.
a+
  • The file is opened for write/read-only.
  • File pointer points to the end of the file.
  • Existing data in the file is preserved.
  • if the file is not there then a new file is created
  • “$use_include_path” is optional, by default is false, if set to true, in the searches the function include path too.
  • “$context” is also optional, we can use to specify the context support.

PHP fwrite Function (PHP write to File)

The fwrite function is used to write files (create a text file in PHP)
it has the general syntax

EXplanation: fwrite in PHP function general syntax

  • “fwrite” is the PHP function which is used for writing to files
  • “$handle” it is the file pinter resource.
  • “$string” is the data to be written in the file.
  • “$length” is optional, it can be used to specify the maximum file length.

PHP Fclose Function

  • “Fclose” function is used to close a file in PHP which is already open.
  • “$handle” is used for the file pointer resource.

it has the general syntax

now, let us see the following example which creates file_settings.txt.
there are the given below the following functions which we will use.

  • Fopen
  • Fwrite
  • Fclose

The code below “create_file_setting_file.php” implements the above example, see the given table

Cases Source Code
Open a file: How to open a PHP file?  
Create a File: How to create a text file in PHP  
Closing a file: How to close a PHP file?  

Copy Function in PHP

Copy function in PHP used to copy the files, there are the following general syntax

  • “$file” it specifies the file path and also the name of the file to be copied
  • “$copied_file” specified the path and name of the copied file.

File Handling in PHP Copy function implementation code:-

Delete a file in PHP

The unlink function is used to delete a file, there is the following code given below illustrates the implementation.

Fgets Function PHP

The fgets function is used to read PHP files line by line, there are the following general syntax

  • “$fgets” it is a function used for reading line by line
  • “$handle” is the file pointer resource.

Lets us see the example which reads my file_settings.txt file using the fopen and gets functions.

the explanation the source code:-

  • “fopen” function which returns the pointer to the file specified in the file path.
  • “die()” function is called if an error occurs, and displayed a message and exist execution of the script.

File_get_contents PHP Function

  • the file_get_contents PHP function is used to read the entire file contents.
  • The difference between file_get_contents and fgets is that file_get_contents returns the file data as string while fgets reads the line by line.

How to Create a file using PHP?

To create a file using touch() function syntax:-

Example:-

How to Delete a file using PHP?

To Delete a file using unlink( ) function Syntax:-

Example:-

How to Rename the file using PHP?

To rename a file using rename( ) Function Syntax:-

Example:-

How to copy a file using PHP?

To copy a file using copy( ) function Syntax:-

Example:-

how to Checks whether a file or directory exists?

To check file or directory existence used file_exists() Function Syntax:-

Example:-

How to Check Path of the file in PHP

realpath( ) function is used to check the real path of the file.

Example:-

Check size of the file in PHP

for check size of the file used filesize( ) function syntax.

Example:-