Re: delete file contents before writing?

From: Date: Mon, 03 Jun 2002 19:43:04 +0000
Subject: Re: delete file contents before writing?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-100459@lists.php.net to get a copy of this message
Try unlink("$file_name") then fopen("$file_name","rw") unlink() deletes the whole file from the directory. Hope this helps. Hugh ----- Original Message ----- From: "Jas" <jlgerfen@hotmail.com> To: <php-general@lists.php.net> Sent: Monday, June 03, 2002 8:37 AM Subject: [PHP] delete file contents before writing? > Not sure how I could do this as I could not find a functions on php.net to > allow me to first delete the entire contents of a file "before" writting the > changes supplied by a textarea. Here is what I have thus far: > Page one - Form: > <?php > $file_name = 'blank.php'; > $open_file = fopen("$file_name","rw"); > if (!$open_file) { > die("cannot open $file_name"); > } else { > $read_file = fread($open_file, filesize($file_name)); > $strip_php = eregi_replace("<","&a&", $read_file); > fclose($open_file); > } > ?> > <form name="edit_passwords" method="post" > action="edit_passwords.php"> > <textarea name="passwords" cols="60" rows="7"><?php > echo $strip_php; > ?></textarea> > <br> > <input type="submit" name="save" value="save"> > </form> > Page two - open file and write changes: > <?php > if (!$passwords) { > header ("Location: index.php"); > } else { > $file_name= "blank.php"; > $file_open = fopen($file_name, "r+"); > eregi_replace("&a&","<", $file_open); > #eregi_replace("\","", $file_open); > fwrite($file_open, $passwords); > print ("$passwords was written to $file_name"); } > ?> > As of right now it opens the file and writes your changes, however it simply > goes to the end of the file then begins writting. I would like to delete > the contents of the file, then write the changes. > Any help or pointers are appreciated! > Jas > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#100459) next »