php

Write into a file in PHP using fwrite()

Write into a file in PHP using fwrite()
Many built-in functions exist in PHP to write in a new file or into the existing file. fwrite() function one of them to write content into the file. fopen() and fclose() functions are required to write content into the file using fwrite() function. fopen() function is used to open a file for reading, writing, and appending that returns a file handler. fwrite() function uses the file handler to write the content in the file. fclose() function is used to close the file that has opened for reading or writing and release the buffer that is used by the file. How the content can be written into a new or an existing file using the fwrite() function has been explained in this tutorial.

Use of fwrite() function:

This function is used to write the particular content or the specific number of bytes in a file after opening it using the fopen() function. The syntax of this function is given below.

Syntax:

int fwrite(resource $file_handler , string $string_data [, int $length ])

This function can take three arguments. The first argument is a file handler variable that is defined before to open the file for writing. The second argument is the string data that will be written into the file. The third argument is optional, and it is used to write the specific number of bytes into the file.

You have to set the write permission to create any file using PHP script on Ubuntu. Run the following command to set all permissions for the fwrite folder where the new file will be created using the fwrite() function.

$ sudo chmod 777 -R /var/www/html/php/fwrite

Example-1: Write the content by creating a new file

The following example shows the way of creating a new file by using the fwrite() function. fopen() function is used in the script to create a file handler for writing a new text file named newfile.txt. Next, the $file_handler variable is used in fwrite() function to write the content of $data variable in the file. fclose() function is used to close the file to release the resource allocated by fopen() function. file_get_contents() function is used to read the content of newfile.txt to confirm that the file has been created and the content has been written properly in that file.

//Define the filename
$filename = "newfile1.txt";
//Open the file for reading
$file_handler = fopen($filename, 'w');
//Check the file handler is created or not
if(!$file_handler)
//Print the error message
die("The file can't be open for writing
");
else

//Write the particular content in the file
$data = "This is the first line of the file.
";
fwrite($file_handler, $data);
//Close the file
fclose($file_handler);
//Print the success message
echo "

The file is created with the content.

";
//Print the file content
echo "

The content of the file after creation:

";
echo file_get_contents($filename);

?>

Output:

The following output will appear after running the script from the server. The output shows that the text, “This is the first line of the file,” is the content of the newfile.txt that has written using the fwrite() function.

Example-2: Append the content into an existing file

The following example shows how to append any existing file's content using the fwrite() function. The text file named newfile.txt created in the previous example has opened with append mode using the fopen() function. Next, the fwrite() function is used to add the content of $data to the end of the file. Like the previous example, the file_get_contents() function is used to check the updated content of newfile.txt.

//Define the filename
$filename = "newfile1.txt";
//Print the file content
echo "

The content of the file before update:

";
echo file_get_contents($filename);
//Open the file for reading
$file_handler = fopen($filename, 'a+');
//Check the file handler is created or not
if(!$file_handler)
//Print the error message
die("The file can't be open for writing
");
else

//Write the particular content in the file
$data = "This is the second line of the file.
";
fwrite($file_handler, $data);
//Close the file
fclose($file_handler);
//Print the success message
echo "

The file is updated with the content.

";

//Print the file content
echo "

The content of the file after update:

";
echo file_get_contents($filename);
?>

Output:

The following output will appear after running the script from the server. The output shows that the text, “This is the second line of the file,” has been added at the end of the text file.

Example-3: Write the specific number of bytes into a file

The particular string data is used for writing into a text file in the previous two examples. But if you want to write the specific bytes of data into a file, you have to use the third argument of the fwrite() function. The following example shows the way of writing the particular bytes of data into a new text file. A file handler named $file_handler is used to create a new file named newfile2.txt, and the fwrite() function is used to write 35 bytes of data into that file. file_get_contents() function is used here to read the content of the file like the previous example.

//Define the filename
$filename = "newfile2.txt";
//Open the file for reading
$file_handler = fopen($filename, 'w');
//Check the file handler is created or not
if(!$file_handler)
//Print the error message
die("The file can't be open for writing
");
else

//Write 35 bytes from the $data
$data = "PHP is a popular scripting language for developing web application.
";
fwrite($file_handler, $data, 35);
//Close the file
fclose($file_handler);
//Print the success message
echo "

The file is created with the content of 35 bytes.

";
//Print the file content
echo "

The content of the file after creation:

";
echo file_get_contents($filename);

?>

Output:

The following output will appear after running the script from the server. The output shows that 35 bytes of the text, “PHP is a popular scripting language for developing web application.” is “PHP is a popular scripting language” that has been written into the file.

Conclusion:

The uses of the fwrite() function with and without the optional argument have been explained in this tutorial by using multiple examples. This tutorial will help the readers to know the one way of writing content into a file, and they will be able to write the content into a file by using the fwrite() function.

Add Mouse gestures to Windows 10 using these free tools
In recent years computers and operating systems have greatly evolved. There was a time when users had to use commands to navigate through file manager...
Control & manage mouse movement between multiple monitors in Windows 10
Dual Display Mouse Manager lets you control & configure mouse movement between multiple monitors, by slowing down its movements near the border. Windo...
WinMouse lets you customize & improve mouse pointer movement on Windows PC
If you want to improve the default functions of your mouse pointer use freeware WinMouse. It adds more features to help you get the most out of your h...