Very often you will find you want to work with a file as if it were a "scratchpad" - a holding area where you can write out temporary data for later use. To make this as easy as possible, PHP has a function called tmpfile() which takes no parameters, but will create a temporary file on the system, fopen() it for you, and send back the file handle as its return value. Show That file is then yours to read from and write to all you wish, and is deleted as soon as you fclose() the file handle or the script ends. Here is an example of tmpfile() in use:
As you can see, tmpfile() is a drop-in replacement for fopen()ing a known file, so it is easy to make use of. If you want to know where these temp files are being saved, use the sys_get_temp_dir() function - it's new in PHP 5.2.1, but returns a string containing the directory used for creating temporary files. Want to learn PHP 7? Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today! If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it! The first argument is mandatory, and it is used to define the filename where the data will be written. The second argument is mandatory and used to define the data written in the file. The third argument is optional, which is used to define opening the file or writing the data. Any of the following values can be used in this argument. FILE_USE_INCLUDE_PATH: FILE_APPEND: LOCK_EX: 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 ExamplesDifferent 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 and write permissions to the folder where the file will be located before practicing the examples of this tutorial; otherwise, the permission denied error would occur. Run the following command to 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 FileCreate a PHP file with the following script that will write a text in a text file located inside the folder, ‘/var/www/html/php’. If the temp.txt file exists before, then the file_put_contents() will overwrite the file’s content. If the temp.txt file 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 check whether the text has been written properly in the file or not. <?php Output: The following output would be appeared after executing the above script if the text was written properly in the file. Example-2: Append Data into an Existing FilenameCreate a PHP file with the following script that will append a text with the new line in an existing text file located inside the folder, ‘/var/www/html/php’. The FILE_APPEND and LOCK_EX options are used in the third argument of the file_put_contents() function for appending the text to the temp.txt file. Next, the file_get_contents() function is used in the script to check whether the text has been written properly in the file or not. <?php Output: The following output would be appeared after executing the above script if the text was appended properly in the file. Example-3: Write/Overwrite the File Content Using FILE_USE_INCLUDE_PATHYou have to enable the include_path directive in the php.ini file for using the FILE_USE_INCLUDE_PATH option, the third argument of the file_put_contents() function. Open the php.ini file and remove the semicolon (;) from the front of the following line. include_path = ".:/usr/share/php" Run the following command from the terminal to restart the apache server after changing the php.ini file. $ sudo service apache2 restart Create a PHP file with the following script that will write/overwrite the text with the newline in an existing text file named myfile.txt that is located in the location defined in include_path directive. Next, the file_get_contents() function has been used in the script to check the text has been written properly in the file or not. <?php Output: The following output would be appeared after executing the above script if the text was written properly in the file. Example-4: Write to a File by Creating a DirectoryCreate a PHP file with the following script that will write/overwrite the text with the new line in a text file named testing.txt located inside the directory named ‘temporary’ by using a user-defined function named WriteIntoFile(). This function will create the directory if the directory does not exist in the current location before writing into the file. Next, the file_get_contents() function is used in the script to check whether the text has been written properly in the file or not. <?php Output: The following output would be appeared after executing the above script if the text was written properly in the file. ConclusionDifferent ways of writing content to a file by using the file_put_contents() function have been shown in the examples of this tutorial to help the PHP users to apply this function properly in their script. |