digixone Posted October 3, 2013 Share Posted October 3, 2013 Hi,I need help on saving email form using fopen/fwrite. I need the character form or the email that will be saved on the top of txt or config file like it will be wriiten only on top for at least 5 lines only and the rest of the content will remain.I am using this: $myFile = "/server/path/location/code/backup/sendstats.conf"; $fh = fopen($myFile, 'r+') or die("can't open file"); $stringData = "MailTo = $slEmail\n"; fwrite($fh, $stringData); $stringData = "MailFrom = $slSender\n"; fwrite($fh, $stringData); $stringData = "\n#\n#\n#"; fwrite($fh, $stringData); } That will write on top for about 4 lines and 3 more line spacing, but when ever long characters have been input in the form, the content on line 10 will be strip off..How can I write and stop the string for about 5 lines only and rest content will remian intact?Thanks Quote Link to comment https://forums.phpfreaks.com/topic/282670-save-write-files-on-top-using-fwritefopen/ Share on other sites More sharing options...
kicken Posted October 3, 2013 Share Posted October 3, 2013 If you want to prepend to the file (write new info to the top but keep the old info) then you need to first read-in the existing file content, blank the file, then write the new content followed by the old content. Using file_get_contents and file_put_contents makes this easier. $file = "/server/path/location/code/backup/sendstats.conf"; $oldData = file_get_contents($file); $newData = "MailTo = {$slEmail}\nMailFrom = {$slSender}\n#\n#\n#"; file_put_contents($file, $newData.$oldData); Quote Link to comment https://forums.phpfreaks.com/topic/282670-save-write-files-on-top-using-fwritefopen/#findComment-1452407 Share on other sites More sharing options...
Barand Posted October 3, 2013 Share Posted October 3, 2013 If, OTOH, you want to replace the first 5 lines $file = "/server/path/location/code/backup/sendstats.conf"; $data = file($file); $data[0] = $new_data . "\n"; . . $data[4] = $other_data . "\n"; file_put_contents($file, join('', $data)); Quote Link to comment https://forums.phpfreaks.com/topic/282670-save-write-files-on-top-using-fwritefopen/#findComment-1452414 Share on other sites More sharing options...
digixone Posted October 3, 2013 Author Share Posted October 3, 2013 Alright.. I'll try those suggestions. I'll post results back.Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/282670-save-write-files-on-top-using-fwritefopen/#findComment-1452430 Share on other sites More sharing options...
digixone Posted October 3, 2013 Author Share Posted October 3, 2013 (edited) If you want to prepend to the file (write new info to the top but keep the old info) then you need to first read-in the existing file content, blank the file, then write the new content followed by the old content. Using file_get_contents and file_put_contents makes this easier. $file = "/server/path/location/code/backup/sendstats.conf"; $oldData = file_get_contents($file); $newData = "MailTo = {$slEmail}\nMailFrom = {$slSender}\n#\n#\n#"; file_put_contents($file, $newData.$oldData); This one works but the last form saved was on the 4th or 5th line, and when I save another string it will not replace the previous one. I want to do it this way..... eg: 1st saved email is: email1@localhost then when I save another one with email2@localhost, then this will replace email1@localhost to email2@localhost the content: ###### config file ###### MailTo = email2@localhost <-- this one will be replaced when a new email to input is saved MailFrom = EMAIL2 <-- this one will be replaced when a new email from input is saved ##### end saved ###### # ##################################################### # below this line shoulb be kept intact ##################################################### This is where the content should be intact coz this one contains the main config settings etc... etc... etc.. and so on.... sorry I am new to this.. but is this called append? instead of prepend? Edited October 3, 2013 by digixone Quote Link to comment https://forums.phpfreaks.com/topic/282670-save-write-files-on-top-using-fwritefopen/#findComment-1452450 Share on other sites More sharing options...
Ch0cu3r Posted October 3, 2013 Share Posted October 3, 2013 (edited) You can open the sendstats.conf file using file. This will load each line in the file to an array Now if you know that MailTo and MailFrom config is always going to be on a certain line, say lines 3 and 4 for example then you could do $config = file('sendstats.conf'); // read lines into array // change lines 3 and 4 to new config $config[2] = "MailTo = $new_MailTo\n"; // rewrite MailTo line $config[3] = "MailForm = $new_mailForm\n"; // rewrire MailForm line // rewrite config back to sendstats.conf file_put_contents('sendstats.conf', implode('', $config)); Edited October 3, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282670-save-write-files-on-top-using-fwritefopen/#findComment-1452453 Share on other sites More sharing options...
Barand Posted October 3, 2013 Share Posted October 3, 2013 Great idea, Ch0cu3r. Just what I said in #3 above. Quote Link to comment https://forums.phpfreaks.com/topic/282670-save-write-files-on-top-using-fwritefopen/#findComment-1452464 Share on other sites More sharing options...
Ch0cu3r Posted October 3, 2013 Share Posted October 3, 2013 Great idea, Ch0cu3r. Just what I said in #3 above. Sorry my bad. Quote Link to comment https://forums.phpfreaks.com/topic/282670-save-write-files-on-top-using-fwritefopen/#findComment-1452465 Share on other sites More sharing options...
Barand Posted October 3, 2013 Share Posted October 3, 2013 No need for such extreme measures, self-flagellation will suffice. Quote Link to comment https://forums.phpfreaks.com/topic/282670-save-write-files-on-top-using-fwritefopen/#findComment-1452468 Share on other sites More sharing options...
digixone Posted October 5, 2013 Author Share Posted October 5, 2013 Hi Gurus... Thanks for sharing some ideas.. I found out that the file I am trying to re-configure has a secondary config file that can be over writen. So I can still use "fwrite($fh, $stringData);"Any way, gonna learn more the other way of saving strings to file and I'll try those suggestions posted here in another project.Thank a lot for the big help Gurus!!! Quote Link to comment https://forums.phpfreaks.com/topic/282670-save-write-files-on-top-using-fwritefopen/#findComment-1452668 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.