PHP FILE_PUT_CONTENTS() FUNCTION

     
Many functions exist in PHP to write nội dung in the file. The file_put_contents() function is one of them. It is used to write any string data in a file. If the tệp tin exists, this function will overwrite the previous content with the new string data or append the string at the kết thúc of the file. If the file does not exist, a new file will be automatically created with the string value. The syntax of this function is given below.

Bạn đang xem: Php file_put_contents() function

Syntax:


Int|false file_put_contents ( string $filename , mixed $data <, int $flags = 0 <, resource $context >> )

The first argument is mandatory, & it is used to define the filename where the data will be written.

The second argument is mandatory và used to define the data written in the file.

The third argument is optional, which is used khổng lồ define opening the file or writing the data. Any of the following values can be used in this argument.

FILE_USE_INCLUDE_PATH:It is used the tìm kiếm the file in the include directory.

FILE_APPEND:If the file exists, it is used to add the data at the end of the file.

Xem thêm: Giải Địa Lí 6 Bài 8: Sự Chuyển Động Của Trái Đất Quanh Mặt Trời

LOCK_EX:It is used lớn set the exclusive lock on the tệp tin when writing.

The fourth argument is optional and used to define the stream’s behavior or the context for handling the file.

The function returns the number of characters written into the file and returns False if no data is written into the file

file_put_contents() Function Examples

Different uses of the file_put_contents() function have been shown in the next part of this tutorial by using multiple examples. You have set the necessary read & write permissions to the thư mục where the tệp tin will be located before practicing the examples of this tutorial; otherwise, the permission denied error would occur. Run the following command lớn set all permission for the folder, ‘/var/www/html/php’.


$ sudo chmod -R 777 /var/www/html/php

Example-1: Write/Overwrite the content into a File

Create a PHP tệp tin with the following script that will write a text in a text tệp tin located inside the folder, ‘/var/www/html/php’. If the temp.txt tệp tin exists before, then the file_put_contents() will overwrite the file’s content. If the temp.txt tệp tin does not exist, then the file_put_contents() function will create the file automatically before writing. Next, the file_get_contents() function is used in the script to kiểm tra whether the text has been written properly in the tệp tin or not.


//Set the filename$filename = "temp.txt";//Set the string value$str = "Adding nội dung to the file.";//Write content to the filefile_put_contents("/var/www/html/php/".$filename, $str) or print_r(error_get_last());//Check the tệp tin exists or notif(file_exists($filename))echo "The content of the file:";//Print the nội dung of the fileecho file_get_contents($filename);?>

Output:

The following output would be appeared after executing the above script if the text was written properly in the file.

Xem thêm: Review Sách Giải Mã Siêu Trí Nhớ Pdf Mai Tường Vân, Giải Mã Siêu Trí Nhớ

*
*
*
*
*

Conclusion

Different ways of writing nội dung to a file by using the file_put_contents() function have been shown in the examples of this tutorial khổng lồ help the PHP users to apply this function properly in their script.