Bidou Cpk
Bidou Cpk

Reputation: 3

(php) fwrite erase and doesn't write

I have a file "tw.txt" with the text "test text" in it.

If I try to write "lol" in "tx.txt" with fwrite, the content ("test text") is simply erased and not replaced.

There is no error displayed by the server, however, I can see my Error: can't write in file.

CHMOD is set to 777 in every files and folders, from the "var" rep to the website folder. If I try to read a file with fopen, no problem. I tried to change the chmod with PHP... no success. I tried to append, it erase.

The code works fine on two other servers.

Any clues ? Thanks.

<?php

ini_set('display_errors', 'On');    
ini_set('allow_url_fopen', '1');
error_reporting(E_ALL);

$fd=fopen("tw.txt","w") or die("Error: can't open file.");  
//chmod("tw.txt", 511);     
fwrite($fd,"lol") or die('Error: can't write in file.');
fclose($fd); 
?>

Upvotes: 0

Views: 1599

Answers (2)

chaitu
chaitu

Reputation: 599

                     $myFile = 'tw.txt';
                     $fh = fopen($myFile, 'w') or die("can't open file");
                     fwrite($fh, 'lol');
                     fclose($fh);

Upvotes: 0

Yannick Blondeau
Yannick Blondeau

Reputation: 9621

Have you tried other opening modes?

If you need to append some data, you should try something like:

$fd=fopen("tw.txt","a+")

Upvotes: 1

Related Questions