- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP exit()
- PHP exit() vs. break
- PHP isset()
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- PHP mysqli: Insert Record
- PHP mysqli: Update Record
- PHP mysqli: Fetch Record
- PHP mysqli: Delete Record
- PHP mysqli: SignUp Page
- PHP mysqli: LogIn Page
- PHP mysqli: Store User Data
- PHP mysqli Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP file_put_contents() function
The PHP file_put_contents() function is used when we need to write or put some data or content into a file. For example:
<?php file_put_contents("myfile.txt", "PHP is Fun! Isn't it?"); ?>
The output produced by the above PHP example on the file_put_contents() function is nothing, but the text "PHP is Fun! Isn't it?" get written in the file myfile.txt available in the current directory. Here is the snapshot of the current directory, along with the opened file myfile.txt, after executing the above PHP example:
Note: If the file already has some content, then the previous content gets overwritten with the new one. But we can use FILE_APPEND to avoid erasing or overwriting the previous content.
Note: If the specified file does not exist, then a new file will be created.
PHP file_put_contents() Syntax
The syntax of the file_put_contents() function in PHP is:
file_put_contents(file, data, mode, context)
The first two parameters (file and data) are required, but the last two parameters (mode and context) are not.
Note: The file parameter is used to specify the name of the file in which we need to write the content.
Note: The data parameter is used to specify the data to put into the file.
Note: The mode parameter is used when we need to specify the way to open the file to put or write data into it. We can specify the way to open the file in any of the following three ways:
- FILE_APPEND: Used when we need to avoid overwriting. That is, if the file already exists with some content inside it, then the new data will get appended.
- FILE_USE_INCLUDE_PATH: Used to search the file in the include_path.
- LOCK_EX: Used to put an exclusive lock on the file while writing content into it.
Note: The context parameter is used to specify the context in which to handle the file.
PHP file_put_contents() example
Consider the following PHP code as an example demonstrating the "file_put_contents()" function:
<?php $file = "myfile.txt"; $content = "Hey,\nWhat's going on?\nIs everything alright?"; if(file_put_contents($file, $content)) echo "<p>The content is written into the file.</p>"; else echo "<p>Unable to write the content into the file.</p>"; ?>
The output of the above PHP example is:
Now let me create another example, with FILE_APPEND as the value of the mode parameter:
<?php $file = "myfile.txt"; $content = "\nYes, everything is Okay.\nThank You!"; if(file_put_contents($file, $content, FILE_APPEND)) echo "<p>The content is written into the file.</p>"; else echo "<p>Unable to write the content into the file.</p>"; ?>
You will get the same output as the previous one after executing this example. And here is the snapshot of the file, myfile.txt, after executing the previous two PHP examples:
« Previous Tutorial Next Tutorial »