Yogesh Chauhan's Blog

How to overwrite file contents with new content in PHP?

in PHP on February 20, 2020

I use file_put_contents for that.

For example,


file_put_contents('file.txt', 'Yogesh');
echo file_get_contents('file.txt'); // Yogesh
file_put_contents('file.txt', 'Chauhan');
echo file_get_contents('file.txt'); // Chauhan

I have tried to do the same with fopen()  and  w+ mode as well. That works pretty well too.

  • 'w' ->     Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

  • 'w+'->     Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

Checkout on PHP manual:

https://www.php.net/manual/en/function.fopen.php


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
How JavaScript classes allows us to do less typing (syntactic sugar)?JavaScriptHow to remove trailing characters from a string using JavaScript?JavaScriptRendering Elements in ReactReactHow to Install Xcode Command Line Tools on MacOS?MiscellaneousLearn to create profile card using HTML and CSSCSSHow to set the permissions on all files and folders in Mac?MiscellaneousHow to create a smooth scrolling effect with CSS?CSSClearwater Seafoods – B2C in ChinaMiscellaneousWordPress: How to display slider repeater fields in ACF?WordPressHow to make HTML form interactive and using CSS?CSSControl Scrolling with CSS Scroll SnapCSSDifference between :where and :is in CSSCSS