Re: [PHP] delete file contents before writing?
On Monday 03 June 2002 23:37, Jas wrote: 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: [snip] 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! Read the manual entry for fopen() and see what it says about the 'w' flag. -- Jason Wong - Gremlins Associates - www.gremlins.com.hk Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * /* Money can't buy love, but it improves your bargaining position. -- Christopher Marlowe */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] delete file contents before writing?
If you really want to delete the file contents then open the file in write-only mode, like so: $open_file = fopen($file_name,w); -Naintara -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] t]On Behalf Of Jas Sent: Monday, June 03, 2002 8:37 AM To: [EMAIL PROTECTED] 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 --- Incoming mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.363 / Virus Database: 201 - Release Date: 05/21/2002 --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.363 / Virus Database: 201 - Release Date: 05/21/2002 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete file contents before writing?
On Mon, 2002-06-03 at 11:37, Jas wrote: 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: Just fopen a file with the 'w' mode when you want to clear/truncate it before writing to it. $file_open($file_name,'w'); ... would probably do what you want. Toby -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete file contents before writing?
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 [EMAIL PROTECTED] To: [EMAIL PROTECTED] 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete file contents before writing?
My mistake: Try unlink() then touch() not fopen(). Hugh - Original Message - From: hugh danaher [EMAIL PROTECTED] To: php [EMAIL PROTECTED] Sent: Monday, June 03, 2002 12:43 PM Subject: Re: [PHP] delete file contents before writing? 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 [EMAIL PROTECTED] To: [EMAIL PROTECTED] 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Delete File Contents?
Just open the file in mode w. $fp = fopen(your_file, w); There is also the the function fseek(), which will allow you to move the file pointer around to any place in the file. Mode w will suffice for your needs. fopen() - http://www.php.net/manual/en/function.fopen.php fseek() - http://www.php.net/manual/en/function.fseek.php Thanks Jeremy Allen -Original Message- From: Chris [mailto:[EMAIL PROTECTED]] Sent: Monday, December 17, 2001 3:57 PM To: [EMAIL PROTECTED] Subject: [PHP] Delete File Contents? I've created a program that runs a process then stores the results to a file on the server. I would like the file to contain only the most recent update, but it seems I can only append to my using fopen. Is there a way to open the file and overwrite the data in that file? I've considered deleting the file and creating a new one but that seems like a lot to do. It would be nice if there were a way to set a pointer to the top of the file and overwrite. Thanks, Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] _ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]