Jump to content

fopen to Add to a file instead of overwrite (replace) content.


blesseld

Recommended Posts

Hey,

 

I am currently using a form that submits Keywords, description, and page title.

 

I currently use a separate file per page... it's a template site that does not have a database, and needs to remain without one. Contains 60 pages

 

Is it possible to save all this data to 1 file instead 1 file per page?  I would need to be able to open the "meta" file and ADD to it instead of Write over/replace what's there....as that's what happens now when you update your meta info.

 

I am using the form to store variables to an array and then using fopen I store the variables to a file.

 

Here is the form that submits the data

$readmeta = file_get_contents("content-page-$cp_number-meta.php");
$metadata = unserialize($readmeta);
extract($metadata);
echo <<< _END
<div class="clear"></div>
	<form method='post' action='admin-submit-meta.php?edit_page=$cp_number'>
		<ul class="edit-form" style="float: left;">
			<li><label class="label">Select Template:</label>
			<select name="updatetemplate">
			</select>
			</li>
		</ul>
		<ul class="edit-form" style="float: right;">
			<li><label class="label-400">Current Template: <span style="color: #f06c32;">$artemplate</span></label></li>
		</ul>
<div class="clear"></div>
		<ul class="edit-form">
			<li><label class="label">Page Name</label><br /><input class="text" type='text' maxlength='300' name='updatemeta0' value='$arpagetitle' /></li>
			<li><label class="label">Title</label><br /><input class="text" type='text' maxlength='300' name='updatemeta1' value='$artitle' /></li>
			<li><label class="label">Keywords</label><br /><input class="text" type='text' maxlength='300' name='updatemeta2' value='$arkeyword' /></li>
			<li><label class="label">Description</label><br /><input class="text" type='text' maxlength='300' name='updatemeta3' value='$ardesc' /></li>
		</ul>
		<ul class="edit-form">
			<li class="edit-form-submit"><input src="../themes/theme-admin/default-admin-template/images/meta-submit.jpg" type='image' value='Login' name='submit' /></li>
		</ul>
	</form>
<div class="clear"></div>
_END;

 

Here is my Action page:

$updatemeta0 = $_POST['updatemeta0'];
$updatemeta1 = $_POST['updatemeta1'];
$updatemeta2 = $_POST['updatemeta2'];
$updatemeta3 = $_POST['updatemeta3'];
$updatetemplate = $_POST['updatetemplate'];
$value = array(
		'arpagetitle' => "$updatemeta0",
            'artitle' => "$updatemeta1",
            'arkeyword' => "$updatemeta2",
            'ardesc' => "$updatemeta3",
            'artemplate' => "$updatetemplate"
        );
$fp = fopen("content-page-1-meta.php", "w");
fwrite($fp,serialize($value));
break;

 

Yes it is possible to append to a flat file and not over write the existing data. Just as well as it would be feasible to do it all out of one file.

 

Ill be honest I didn't take a grand look at your code posted so im not exactly to sure as to how it would be saved in the flat file. But you have a couple options. as far as flat files go. 1) you save all your data thats going into it in a CSV format and add something to it to identify each one like an ID so altering that specific one should that be a need later will be a bit easier. as well as maybe help out preventing duplicate entries. This would require use of explode() and implode() and using foreach() regularly to handle the file or files.

 

Another option is looking into parse_ini_file() and creating an INI like file or files

 

One big issue with either a file or multiple files is size. Remember the more you add to a flat file the bigger it gets, the bigger it gets the slower your script runs. Another issue is anyone can just hop along and download your flat file and make it there own.

 

Well I know this wasn't a huge help, but I hope it gives you food for thought.

Well I know this wasn't a huge help, but I hope it gives you food for thought.

 

Thanks,

 

Definitely gives me a sense of direction on how I can achieve my end result.  I will do some research and see what I can come up with.  It's kinda like....if it's not to to daunting I will add it, if not I won't bother.  Main reason there is NO database.... and I am making a DB version soon...things will be significantly easier using MySQL.

overwrites

$fp = fopen("content-page-1-meta.php", "w");

appends

$fp = fopen("content-page-1-meta.php", "a");

•"r" (Read only. Starts at the beginning of the file)

•"r+" (Read/Write. Starts at the beginning of the file)

•"w" (Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist)

•"w+" (Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist)

•"a" (Write only. Opens and writes to the end of the file or creates a new file if it doesn't exist)

•"a+" (Read/Write. Preserves file content by writing to the end of the file)

•"x" (Write only. Creates a new file. Returns FALSE and an error if file already exists)

•"x+" (Read/Write. Creates a new file. Returns FALSE and an error if file already exists)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.