Learn PHP from Scratch Day -12
PHP File() Function: File_exists, Fopen, Fwrite, Fclose, Fgets, copy, unlink
What is a File?
A file is simply a resource for storing information on a computer.
Files are usually used to store information such as;
- Configuration settings of a program
- Simple data such as contact names against the phone numbers.
- Images, Pictures, Photos, etc.
File Formats Supported by PHP
PHP supports wide range of filetypes , from text files to image files , from excel files to database files. PHP has different libraries that helps us to work with file types. It supports basic operations like create file , read , delete and edit file. You want to store program settings i.e. program.ini
PHP files Functions
PHP provides a convenient way of working with files via its rich collection of built in functions.
Operating systems such as Windows and MAC OS are not case sensitive while Linux or Unix operating systems are case sensitive.
Adopting a naming conversion such as lower case letters only for file naming is a good practice that ensures maximum cross platform compatibility.
Let's now look at some of the most commonly used PHP file functions.
PHP File_exists Function
This function is used to determine whether a file exists or not.
- It comes in handy when we want to know if a file exists or not before processing it.
- You can also use this function when creating a new file and you want to ensure that the file does not already exist on the server.
The file_exist function has the following syntax.
- "file_exists()" is the PHP function that returns true if the file exists and false if it does not exist.
- "$file_name" is the path and name of the file to be checked
The code below uses file_exists function to determine if the file test.txt exists.
Save the above code in a file named file_function.php Assuming you saved the file in phptuts folder in htdocs, open the URL http://localhost/phptuts/file_function.php in your browser You will get the following results.
test.txt does not exist
PHP Fopen Function
The fopen function is used to open files. It has the following syntax
- "fopen" is the PHP open file function
- "$file_name" is the name of the file to be opened
- "$mode" is the mode in which the file should be opened, the table below shows the modes
- "$use_include_path" is optional, default is false, if set to true, the function searches in the include path too.
- "$context" is optional, can be used to specify the context support.
PHP Fwrite Function
The fwrite function is used to write files.
It has the following syntax
- "fwrite" is the PHP function for writing to files
- "$handle" is the file pointer resource
- "$string" is the data to be written in the file.
- "$length" is optional, can be used to specify the maximum file length.
PHP Fclose Function
Is is used to close a file in php which is already open
It has the following syntax.
- "fclose" is the PHP function for closing an open file
- "$handle" is the file pointer resource.
Let's now look at an example that creates my_settings.txt.
We will use the following functions.
The code below "create_my_settings_file.php" implements the above example.
PHP files operations sample code Image Source : Google Search
PHP Fgets Function
The fgets function is used to read php files line by line. It has the following basic syntax. fgets($handle); HERE,
- "$fgets" is the PHP function for reading file lines
- "$handle" is the file pointer resource.
Let's now look at an example that reads my_settings.txt file using the fopen and fgets functions.
The code below read_my_settings.php implements the above example.
- "fopen" function returns the pointer to the file specified in the file path
- "die()" function is called if an error occurs. It displays a message and exists execution of the script
PHP Copy Function
The PHP copy function is used to copy files. It has the following basic syntax. copy($file,$copied_file); HERE,
- "$file" specifies the file path and name of the file to be copied.
- "copied_file" specified the path and name of the copied file
The code below illustrates the implementation
Deleting a file
The unlink function is used to delete the file. The code below illustrates the implementation.
PHP File_get_contents Function
The file_get_contents function is used to read the entire file contents.
The code below illustrates the implementation.
The difference between file_get_contents and fgets is that file_get_contents returns the file data as a string while fgets reads the file line by line.
- A file is a resource for storing data
- PHP has a rich collection of built in functions that simplify working with files.
- Common file functions include fopen, fclose, file_get_contents
- The table below shows a summary of the functions covered