Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Filesystem fwrite() Function



The PHP Filesystem fwrite() function is used to write content to a file given. To use fwrite(), a file must be opened. So you can use the fopen() function.

After a file has been opened, you may use fwrite() to write text or data into it. You provide the file handle (the one returned by fopen()) and the content you want to write. After writing, the file must be closed using fclose().

Syntax

Below is the syntax of the PHP Filesystem fwrite() function −

fwrite ( resource $handle , string $string [, int $length ] ) : int|false

Parameters

Below are the required and optional parameters of the fwrite() function −

Sr.No Parameter & Description
1

$handle(Required)

It is the file you want to write to.

2

$string(Required)

This is the content you want to write to the file.

3

$length(Optional)

It is the number of bytes from $string that you want to write.

Return Value

The function fwrite() returns the number of Bytes written to the file or FALSE if is fails.

PHP Version

The fwrite() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7 and PHP 8.

Example

We have used the PHP Filesystem fwrite() function to write to a file in the below code. So the file "myfile.txt" is opened in write mode ("w"), content "Hello, world!" is written to the file using fwrite() and then the file is closed with fclose() function.

<?php
   // Open the file in writing mode
   $file = fopen("/PhpProjects/myfile.txt", "w");

   //Specify the content want to write
   $content = "Hello, world!";
   
   // Writes "Hello, world!" to the file
   fwrite($file, $content); 
   
   // Closes the file 
   fclose($file); 

   echo "The content is written successfully.";
?>

Output

Here is the outcome of the following code −

The content is written successfully.

Example

This PHP example opens the existing file "myfile.txt" in append mode, adds some additional text content to it, and then closes the file.

<?php
   // Open file in append mode
   $file = fopen("/PhpProjects/myfile.txt", "a");

   // Text to append
   $content = "This is an appended text.\n";

   // Append content to file
   fwrite($file, $content);

   // Close the file
   fclose($file);

   echo "Text appended to file.";
?> 

Output

This will produce the following result −

Text appended to file.

Example

In this PHP code, a file named "binary_data.bin" is opened in write mode for writing binary data. Some binary data is created using the pack() function and then written to the file. Finally, the file is closed.

<?php
   // Open file in write mode for binary data
   $file = fopen("binary_data.bin", "wb");

   // Binary data to write
   $data = pack("S*", 1990, 2024, 509, 1024);

   // Write binary data to file
   fwrite($file, $data);

   // Close the file
   fclose($file);

   echo "Binary data written to file.";
?> 

Output

This will generate the below result −

Binary data written to file.

Note

The PHP function fwrite() raises E_WARNING on failure.

Summary

The fwrite() function is a powerful function for file manipulation in PHP that is needed for various purposes, like data storage, logging, and the generation of dynamic content.

php_function_reference.htm
Advertisements