Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

382
Views
how to write into a text file in php without delete all the data

how can I write into text file without erase all the existing data? I tried this

$txt = 'srge';
$file = fopen('text.txt','w');
fwrite($file,$txt);

but it's not working, it's earse everything

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Note: This will only work when you have appropriate permission for test.txt else it will say

permission denied (un-appropriate will lead to this)

Here we are using:

1. a which is for append this will append text at the end of file.

2. instead of w, flag w is for write, which will write on file without caring about you existing data in that file.

PHP code:

<?php
ini_set('display_errors', 1);
$txt = 'srge';
$file = fopen('text.txt','a');
fwrite($file,$txt);
about 4 years ago · Santiago Trujillo Report

0

according to php documentation:

while you are using :

'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.

try instead:

'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() has no effect, writes are always appended.

about 4 years ago · Santiago Trujillo Report

0

Try with following code

$txt = 'srge';
$file = fopen('text.txt','a');
fwrite($file,$txt);

Writing or Appending to a File

The processes for writing to or appending to a file are the same. The difference lies in the fopen() call. When you write to a file, you should use the mode argument "w" when you call fopen():

$fp = fopen( "test.txt", "w" );

All subsequent writing will occur from the start of the file. If the file doesn't already exist, it will be created. If the file already exists, any prior content will be destroyed and replaced by the data you write.

When you append to a file, you should use mode "a" in your fopen() call:

$fp = fopen( "test.txt", "a" );

For more details please refer this : File operation example

Php Filesystem Functions

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!