Skip to content
Related Articles

Related Articles

Improve Article

How to write Into a File in PHP ?

  • Last Updated : 30 Apr, 2021
Geek Week

In this article, we are going to discuss how to write into a text file using the PHP built-in fwrite() function.

The fwrite() function is used to write into the given file. It stops at the end of the file or when it reaches the specified length passed as a parameter, whichever comes first. 

Syntax:

fwrite(file_name, string)
  • file_name: The name of the input file
  • string: The content to be written.

Return Value: It returns the number of bytes written on success, or false on failure.

Example 1 :



PHP




<?php
// Open a file named geeks_data in write mode
$data = fopen("geeks_data.txt", "w");
  
// writing content to a file using fwrite() function
echo fwrite($data, "This data is added as extra");
  
// closing the file
fclose($data);
?>

Output:

27

Example 2:

PHP




<?php
// Open a file named geeks_data in write mode
$data = fopen("geeks_data.txt", "w");
  
// writing content to a file using fwrite() function
echo fwrite($data, "This data is added as extra 
                    along with previous data");
  
// closing the file
fclose($data);
?>

Output:

52
My Personal Notes arrow_drop_up
Recommended Articles
Page :