Full Stack Web Development Internship Program
- 3k Enrolled Learners
- Live Class
This article will introduce you an easy but an important concept that is how to write a file in PHP. Following pointers will be covered in this article,
Firstly, we need to open the file. There are different attributes that you can give when you open a file.
First thing is to create a handle which will open the file using fopen function. It create the file if it does not exist. We give file name in the first argument. In the second argument we can either write or read or append to the file.
$handle= fopen(”, ”);
In this instance, i want to write to the file so i will use
<?php $handle=fopen('file.txt','w'); ?>
I don’t have file.txt before executing the code. After execution, it gets created
We don’t need to create the variable again as we have created it already. Variable is important because when we want to write to a file, we need to address the handle.
In order to write to a file, we use fwrite() and in the first parameter, we need to specify where we are going to write file.txt($handle) and in second parameter, we will specify the data to be written
fwrite($handle, ' '); <?php $handle=fopen('file.txt','w'); fwrite($handle, 'HelLo ashok, your file is successfully written'); ?>
After executing the file, data will be successfully written into file.txt
Finally, we need to use fclose which closes the connection and we need to pass the address of the file through variable as parameter in fclose()
fclose($handle); <?php $handle=fopen('file.txt','w'); fwrite($handle, 'HelLo ashok, your file is successfully written'); fclose($handle); ?>
It is similar to calling fopen(), fwrite() and fclose() successively in order to write data to a file. In case If filename does not exist, the file will be created as we have seen the same functionality in fopen(). Else, the existing file is overwritten, unless the FILE_APPEND flag is set.
File: It specifies the path to the file where to write the data.
Data: It specifies the data to be written. It can be either a string, an array or a stream resource. The remaining buffer of that stream will be copied to the specified file, If the data is a stream resource. Usage is similar to stream_copy_to_stream(). The data parameter can also be specified as a single dimension array.
Flag: It is an optional parameter which specifies how to open/write to the file. Available flags
File_Append: It appends the data to the file instead of overwriting it, If file filename already exists.
File_Use_Include_Path: It searches for filename in the include directory.
Lock_Ex: It acquires an exclusive lock on the file while proceeding to the writing.
Context: It is an optional parameter which specifies the context of the file handle.Basically it is a set of options that can modify the behavior of a stream.
<?php $file= 'new.txt'; // It will open the file to get existing content $current = file_get_contents($file); // Append below data to the file $current.="This is appended datan"; // It will write the contents back to the file file_put_contents($file, $current); ?>
I don’t have new.txt before executing the code. After execution, it gets created
This brings us to the end of this article on Write A File In PHP.
If you found this blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
Got a question for us? Please mention it in the comments section of ”Write A File In PHP” and I will get back to you.