Text File Update With PHP

Walt, this is a bit of a rhetorical question, but, have you run across a script that allows you to update a SSI text file via a form?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Well, yes, that Chat thing I made yesterday used a text file for its
data store for about 15 minutes, so it’s really pretty simple:

<?php
if(isset($_POST['your_form_field']) && !empty($_POST 

[‘your_form_field’])){
//this next line will strip ANY HTML – beware if you need some
$text = trim(strip_tags($_POST[‘your_form_field’]));
file_put_contents(‘/path/to/file’,$text,LOCK_EX);
print(‘Text updated’);
}
?>

Now understand that if you can write to this file, anyone else on
the same box can as well, unless your server is set up to run your
files in your own sandbox (better ones are, ask your hosting provider
if they offer ‘suexec’ as an option or standard).

You’ll also want to make this file you are updating live somewhere
outside of the Apache document root, which means that you will not be
able to include the file using the normal Apache SSI mechanism. Since
you’re already in the realm of PHP by now, you can simply get the
content and stream it this way:

<?php
$text = file_get_contents('/path/to/file');
print $text;
?>

Does that help?

Walter

On Oct 21, 2008, at 4:47 PM, chuckamuck wrote:

Walt, this is a bit of a rhetorical question, but, have you run
across a script that allows you to update a SSI text file via a form?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Sometime around 21/10/08 (at 16:47 -0400) chuckamuck said:

Walt, this is a bit of a rhetorical question, but, have you run
across a script that allows you to update a SSI text file via a form?

This isn’t difficult to do yourself - once you’ve mastered some basic
PHP. In a nutshell, you’d make a simple PHP form handling script
(which could be done easily in a dozen lines), then write that data
to a predefined file - which you use for your SSI or an equivalent
PHP include. I’ve done this before.

You should take security into consideration, as that could be used by
someone unscrupulous to do just about anything you care to consider
by entering their own scripts… however, there are fairly simple
things you can do to counter that, including password-protecting
wherever that form page lives, requiring a password to be sent in the
form, and stripping code-specific characters from the text.

I’m in the middle of preparing lectures so I can’t provide specific
code examples, but I will mention my “beginner’s guide to PHP”,
written to get people really started. (Every PHP book I’ve seen
assumes far too much existing understanding; getting started from
scratch can be a real hurdle.)

If you can find the mental space to give it a try I think you’ll find
it like being handed a whole new set of power tools after struggling
along with the same old hand tools.

http://www.thehelpful.com/php/

k


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Oooh, good point Walt. I should have looked more closely at that.

Keith: Yeah… i’ve got this giant book of php fairy tales sitting right here but I haven’t gotten far past the credit page. Afraid of nightmares I suppose… I also have had your simple tutorial for quite awhile now which I have read. I guess I need to make some time available for play. PHP include…hmmm


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Sometime around 21/10/08 (at 17:27 -0400) chuckamuck said:

I guess I need to make some time available for play.

That is the secret: playing around with it, trying to do very basic things.

k


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

The security points are well made and I should mention that I intended to put the form on a password protected page.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options