PHP fputs( ) Function Last Updated : 23 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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. file: It is a mandatory parameter that specifies the file.string: It is a mandatory parameter that specifies the string to be written.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. ExceptionsBoth binary data, like images and character data can be written with this function since fputs() is binary-safe.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 : 35Input : $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 : GeeksforgeeksExamples of PHP fputs() Function Below programs illustrate the fputs() function: Example 1: Writing a Full String to a FileIn 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:35Program 2: Writing a Specific Length of a String to a FileThis 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 Comment More infoAdvertise with us Next Article PHP fputs( ) Function S Shubrodeep Banerjee Follow Improve Article Tags : Misc Web Technologies PHP PHP-file-handling PHP-function +1 More Practice Tags : Misc Similar Reads PHP basename( ) Function The basename() function in PHP is an inbuilt function which is used to return the base name of a file if the path of the file is provided as a parameter to the basename() function. Syntax: string basename ( $path , $suffix ) Parameters: The basename() function in PHP accepts two parameters which are 2 min read PHP chgrp( ) Function The chgrp() function in PHP is an inbuilt function that is used to change the user group of the specified file. It returns true on success and false on failure. Only the superuser has the right to change the group of a file arbitrarily. Syntax: bool chgrp ( $filename, $group ) Parameters: The chgrp( 2 min read PHP chmod( ) Function The chmod() function in PHP is an inbuilt function which is used to change the mode of a specified file to a specific mode given by the user. The chmod() function changes the permissions of the specified file and returns true on success and false on failure. Syntax: bool chmod ( string $filename, i 2 min read PHP chown( ) Function The chown() function in PHP is an inbuilt function which is used to change the owner of the specified file. It returns true on success and false on failure. Only the superuser has the right to change the owner of a file. Syntax: bool chown ( $filename, $user ) Parameters: The chown() function in PHP 2 min read PHP copy( ) Function The copy() function in PHP is an inbuilt function which is used to make a copy of a specified file. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure. Syntax: bo 2 min read PHP dirname( ) Function The dirname() function in PHP is an inbuilt function which is used to return the directory name of a given path. The dirname() function is used to parent directory's path i.e levels up from the current directory. The dirname() function returns the path of a parent directory which includes a dot ('.' 2 min read PHP disk_free_space( ) Function The disk_free_space() function in PHP is an inbuilt function which is used to return the amount of free space in a specified directory. The disk_free_space() function denotes the free space in bytes. It returns the available space on a filesystem or on a disk partition. The disk_free_space() functio 2 min read PHP disk_total_space( ) Function The disk_total_space() function in PHP is an inbuilt function which is used to return the total space of a specified directory. The disk_total_space() function denotes the total space in bytes. It returns the total space on a filesystem or on a disk partition. The disk_total_space() function returns 2 min read PHP fclose() Function The fclose() function in PHP closes a file that was previously opened by fopen(). Closing a file releases the resource associated with it and makes sure all the data written to the file is properly saved. Not closing files can lead to resource leaks or incomplete data writing.Syntax:bool fclose(reso 2 min read PHP feof( ) Function The feof() function in PHP is an inbuilt function which is used to test for the end-of-file on a file pointer. It checks if the "end-of-file" has been reached or not. The feof() function is used for looping through the content of a file if the size of content is not known beforehand. The feof() func 2 min read Like