Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

383
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda