YogeshChauhan . com

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

amazon

Most Read

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

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How to create a horizontal scrolling menu with and without flex using CSS (or SCSS)?CSS$this Keyword in PHPPHPSteps to Secure a VPNMiscHow to create a CSS full page background image?CSS3 ways to pass a variable in url() function in SCSS (Sass)SCSSHow to create a sticky menu pin using CSS?CSSHow to create a pricing table using flex in CSS?CSSWhat’s Interpolation in SCSS (Sass)?SCSSOOP, Class and Objects Strategies For Beginners (PHP)PHPHow to Use SQL MAX() Function with Dates?SQL/MySQLWhat are Class Constants in PHP?PHPinclude, include_once, require, require_once in PHPPHP