Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

386
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda