File Handling in PHP | File Management in PHP With All Operations
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.
|
1 2 3 |
<?php file_exists($filename); ?> |
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.
|
1 2 3 4 5 6 7 8 9 10 |
<?php if (file_exists('file_settings.txt')) { echo 'file found!'; } else { echo 'file_settings.txt does not exist'; } ?> |
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:-
|
1 2 3 |
<?php fopen($file_name,$mode,$use_include_path,$context); ?> |
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 |
|
| w+ |
|
| r |
|
| r+ |
|
| a |
|
| a+ |
|
- “$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
|
1 2 3 |
<?php fwrite($handle, $string, $length); ?> |
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
|
1 2 3 |
<?php fclose($handle); ?> |
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
|
1 2 3 |
<?php copy($file,$copied_file); ?> |
- “$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:-
|
1 2 3 4 |
<?php copy('file_settings.txt', 'file_settings_backup.txt') or die("Could not copy a file"); echo "File successfully copied to 'file_settings_backup.txt'"; ?> |
Delete a file in PHP
The unlink function is used to delete a file, there is the following code given below illustrates the implementation.
|
1 2 3 4 5 6 7 8 9 10 |
<?php if (!unlink('file_settings_backup.txt')) { echo "Could not delete a file"; } else { echo "File 'file_settings_backup.txt' successfully deleted"; } ?> |
Fgets Function PHP
The fgets function is used to read PHP files line by line, there are the following general syntax
|
1 2 3 |
<?php fgets($handle); ?> |
- “$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.
|
1 2 3 4 5 |
<?php $fm = fopen("file_settings.txt", 'r') or die("File does not exist or you lack permission to open it"); $line = fgets($fm); echo $line; fclose($fm); ?> |
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.
|
1 2 3 4 5 |
<?php echo "<pre>"; // its Enables displayed of line feeds echo file_get_contents("file_settings.txt"); echo "</pre>"; // Terminates pre tag ?> |
How to Create a file using PHP?
To create a file using touch() function syntax:-
|
1 2 3 |
<?php touch("Nameoffile with extension"); ?> |
|
1 2 3 4 5 6 7 8 9 10 |
<?php //create an micro-soft word file touch("documents.doc"); //create text file touch("documents.txt"); //create pdf file touch('documents.pdf'); ?> |
How to Delete a file using PHP?
To Delete a file using unlink( ) function Syntax:-
|
1 2 3 |
<?php unlink("Nameoffile with extension"); ?> |
|
1 2 3 4 5 6 7 8 9 10 |
<?php //delete documents word file unlink("documents.doc"); //delete text file unlink("documents.txt"); //delete pdf file unlink('documents.pdf'); ?> |
How to Rename the file using PHP?
To rename a file using rename( ) Function Syntax:-
|
1 2 3 |
<?php rename("old fileName with extension","New fileName with the same extension"); ?> |
|
1 2 3 4 5 6 7 8 9 10 |
<?php //rename documents doc rename("documents.doc","Update salary.doc"); //rename text file rename("documents.txt","update salary.txt"); //rename pdf file rename("documents.pdf","update salary.pdf"); ?> |
How to copy a file using PHP?
To copy a file using copy( ) function Syntax:-
|
1 2 3 |
<?php copy("source_file with extension","destination fileName with the same extension"); ?> |
|
1 2 3 4 5 6 7 |
<?php //copy documents doc copy("documents.doc","Update documents.doc"); //copy text file copy("documents.txt","update documents.txt"); ?> |
how to Checks whether a file or directory exists?
To check file or directory existence used file_exists() Function Syntax:-
|
1 2 3 4 5 6 7 |
<?php //check file existence file_exists("NameofFile with extension"); // OR //check directory extistence file_exists("directory name"); ?> |
|
1 2 3 4 |
<?php //check file existence echo file_exists("Update documents.doc"); ?> |
How to Check Path of the file in PHP
realpath( ) function is used to check the real path of the file.
|
1 2 3 |
<?php realpath("fileName with extension"); ?> |
|
1 2 3 4 |
<?php //check real path of the file echo realpath("Update documents.pdf"); ?> |
Check size of the file in PHP
for check size of the file used filesize( ) function syntax.
|
1 2 3 |
<?php filesize("fileName with extension"); ?> |
|
1 2 3 4 |
<?php //check file size echo filesize("Update documents.pdf")." Bytes"; ?> |