Jump to content

Save Write Files on Top using Fwrite/Fopen


digixone

Recommended Posts

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

Link to comment
Share on other sites

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);
Link to comment
Share on other sites

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 by digixone
Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

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!!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.