Many built-in functions exist in PHP to lớn write in a new file or into the existing file. Fwrite() function one of them lớn write nội dung into the file. Fopen() & fclose() functions are required to write nội dung into the tệp tin using fwrite() function. Fopen() function is used to xuất hiện a file for reading, writing, và appending that returns a tệp tin handler. Fwrite() function uses the file handler lớn write the content in the file. Fclose() function is used lớn close the tệp tin that has opened for reading or writing và release the buffer that is used by the file. How the content can be written into a new or an existing tệp tin using the fwrite() function has been explained in this tutorial.

Bạn đang xem: Php write file create/write, how to write into a file in php

Use of fwrite() function:

This function is used to lớn write the particular nội dung or the specific number of bytes in a tệp tin after opening it using the fopen() function. The syntax of this function is given below.


This function can take three arguments. The first argument is a file handler variable that is defined before to mở cửa 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 khổng lồ write the specific number of bytes into the file.

You have lớn set the write permission to create any file using PHP script on Ubuntu. Run the following command to lớn set all permissions for the fwrite thư mục where the new tệp tin will be created using the fwrite() function.

Example-1: Write the nội dung 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 khổng lồ create a file handler for writing a new text tệp tin named newfile.txt.

Xem thêm: Phần Mềm Pqmagic - Norton Partitionmagic 8

Next, the $file_handler variable is used in fwrite() function khổng lồ write the content of $data variable in the file. Fclose() function is used to lớn close the tệp tin to release the resource allocated by fopen() function. File_get_contents() function is used lớn read the content of newfile.txt to confirm that the file has been created and the nội dung has been written properly in that file.

//Define the filename$filename = "newfile1.txt";//Open the tệp tin for reading$file_handler = fopen($filename, "w");//Check the tệp tin handler is created or notif(!$file_handler) //Print the error message die("The file can"t be xuất hiện for writing");else //Write the particular nội dung in the tệp tin $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 nội dung of the tệp tin after creation:

"; echo file_get_contents($filename);?>


The following output đầu ra 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 nội dung of the newfile.txt that has written using the fwrite() function.


I am a trainer of website programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. Are published: Tutorials4u Help.