Open In App

PHP fputs( ) Function

Last Updated : 23 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The fputs() function in PHP is an inbuilt function which is used to write to an open file. The fputs() function stops at the end of the file or when it reaches the specified length passed as a parameter, whichever comes first. The file, string, and the length that has to be written are sent as parameters to the fputs() function and it returns the number of bytes written on success, or FALSE on failure. The fputs() function is an alias of the fwrite() function.

Syntax:

fputs(file, string, length)

Parameters:

The fputs() function in PHP accepts three parameters.

  1. file: It is a mandatory parameter that specifies the file.
  2. string: It is a mandatory parameter that specifies the string to be written.
  3. length: It is an optional parameter that specifies the maximum number of bytes to be written.

Return Value:

  • It returns the number of bytes written on success, or False on failure.

Exceptions

  1. Both binary data, like images and character data can be written with this function since fputs() is binary-safe.
  2. fputs() function without the length parameter writes all data up to the end but it does not include the first 0th byte it encounters.

Examples:

Input : $myfile = fopen("gfg.txt", "w");
echo fputs($myfile, "Geeksforgeeks is a portal of geeks!");
fclose($myfile);
Output : 35

Input : $myfile = fopen("gfg.txt", "w");
echo fputs($myfile, "Geeksforgeeks is a portal of geeks!", 13);
fclose($myfile);
fopen("gfg.txt", "r");
echo fread($myfile, filesize("gfg.txt"));
fclose($myfile);
Output : Geeksforgeeks

Examples of PHP fputs() Function

Below programs illustrate the fputs() function:

Example 1: Writing a Full String to a File

In this example, we use the fputs() function to write an entire string to a file. The program opens the file, writes the string “Geeksforgeeks is a portal of geeks!” to it, and then displays the number of bytes written to the file.

php
<?php
// Opening a file
$myfile = fopen("gfg.txt", "w");

// writing content to a file using fputs
echo fputs($myfile, "Geeksforgeeks is a portal of geeks!");

// closing the file
fclose($myfile);
?>

Output:

35

Program 2: Writing a Specific Length of a String to a File

This example shows how to use the optional length parameter in the fputs() function to write only a portion of a string to a file. The program writes the first 13 bytes of the string “Geeksforgeeks is a portal of geeks!” and then reads and displays the contents of the file.

php
<?php
// Opening a file
$myfile = fopen("gfg.txt", "w");

// writing content to a file with a specified string length using fputs
echo fputs($myfile, "Geeksforgeeks is a portal of geeks!", 13);

// closing the file
fclose($myfile);

//opening the same file to read its contents        
fopen("gfg.txt", "r");
echo fread($myfile, filesize("gfg.txt"));

// closing the file
fclose($myfile);
?>

Output:

Geeksforgeeks

Next Article

Similar Reads

three90RightbarBannerImg
  翻译: