client edidit of text inlcude

How easy is it to set up a PHP page so a client can edit a text file include:

  <?php include("text.txt"); ?>

I would need:

  1. A PHP page to display the content of the text file

  2. A form so the client can upload and overwrite the contents of the text file.

I suppose the text files needs to display in a text area with an upload button.

But, how should the code appear, to open, and overwrite the text file contents?


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

Sorry spelling errors all over the place where is the edit button?


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

Sometime around 20/1/08 (at 09:16 -0500) dwn said:

Sorry spelling errors all over the place where is the edit button?

Don’t worry about the spelling errors.
And don’t look for an edit button either! This forum is as much an
email interface as it is a web forum.

What’s said is said, it can’t be erased. But don’t worry, nobody here
will think ill of you for making a few typing or spelling slips.

:slight_smile:

k


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

Its the duplicated text at the bottom distracting me. It a bit like having your own voice echoing on a Skype call.

It might be better to have a dedicated preview button before posting.

(Anyway back to to subject)

Any ideas how to edit a text file using PHP alone?


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

Its OK I found this:-

 <form action="processscript.php" method="post">
 <textarea rows="25" cols="80" name="content">
 <?
 $fn = "text.txt";
 print htmlspecialchars(implode("",file($fn)));
 ?> 
 </textarea><br>
 <input type="submit" value="Change!"> 
 </form>

 <?
 $fn = "text.txt";
 $content = stripslashes($_POST['content']);
 $fp = fopen($fn,"w") or die ("Error opening file in write mode!");
 fputs($fp,$content);
 fclose($fp) or die ("Error closing file!");
 ?>

It seems to work OK, if anyone else has a similar problem


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

If anyone is interested it works really well. You can even put in HTML markup to style the text. Sort of a low cost CMS system.


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

But saying that, I wouldn’t mind any PHP guru to look over the code to see if there is anything to watch for before I release it in the wild web :-


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

e.g how to set a file size limit, or a character limit


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

I notice that when the file is written, it does not retain any line returns.

Does anyone know how to keep the line returns in without typing html codes? or explain the following function?

print htmlspecialchars(implode("",file($fn)));

It will retain line returns if the following is included

  <br>

after entering every text line in the text area


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

Try adding one more layer to the cake:

print nl2br(htmlspecialchars(implode("",file($fn))));

Let’s take that one-liner a step at a time:

nl2br converts each newline to a break plus a newline.
htmlspecialchars converts any characters that have special meaning
to HTML into entity escapes, so they don’t mess with the rest of your
layout.
implode with an empty delimiter means convert this array into a
string, inserting nothing between each element of the array.
file means read the named file, converting it into an array, broken
into elements at each newline.

Now it occurs to me as I write this, that what you really want is
this, which is much simpler:

print nl2br(htmlentities(file_get_contents($fn)));

The whole business of converting the file into an array and then
right back into a string is a total waste of cycles.
file_get_contents is a very memory-conservative way to read an
entire text file into a variable. It is highly tuned and very very
fast on every platform PHP runs on, and can open files up to the
limit imposed on PHP by your system administrator. I use it on the
Online Library of Liberty to read in up to 20MB XML files before
parsing them to XHTML and then SQL.

Walter

On Jan 20, 2008, at 3:22 PM, dwn wrote:

print htmlspecialchars(implode("",file($fn)));

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

Thanks Walter - your a star.

I’ll need to re-read it a few times to take in what going on.

What happens if you want to read/load/upload a picture file - is that a complex procedure?


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

Walter how should I use…

get_file_contents

Just “as is” and put into a variable? e.g.

$contents = get_file_contents(text.txt)

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

Sorry did not read correctly you added the get_file_contents in the second statement

Thanks


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

After testing both lines:

this one add a space then line return to the end of the file on every edit/save

print htmlspecialchars(implode("",file($fn)));

And this one create the same, plus a space then line return on “every” line
creating double, triple… line space on every save

print nl2br(htmlentities(file_get_contents($fn)));

How does the extra (space then line return) creep in? I notice the text area always has a space then line return even, when nothing is in the text.text file. Could Freeway be adding it?


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

i.e I’m using Freeway pages with this in markup items


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

If you test this code in a PHP file called processscript.php and run it:

<? $fn = "text.txt"; print htmlspecialchars(implode("",file($fn))); ?>
<? $fn = "text.txt"; $content = stripslashes($_POST['content']); $fp = fopen($fn,"w") or die ("Error opening file in write mode!"); fputs($fp,$content); fclose($fp) or die ("Error closing file!"); ?>

Note how when the text area is generated, there is a space then line return already present. How can I remove this so it is totally blank?
(refer to the text.txt file generated - this is blank)


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

This and the other errors you are seeing sound a lot like you have
Mac line-endings on your file, and are running this on a Unix server.
Mac line-endings are rn (return + newline) while Unix line-endings
are simply n (newline).

Try changing the line-endings on your form processor script, and also
on your Freeway page.

Walter

On Jan 21, 2008, at 7:51 AM, dwn wrote:

If you test this code in a PHP file called processscript.php and
run it:

<? $fn = "text.txt"; print htmlspecialchars(implode("",file($fn))); ?>
<? $fn = "text.txt"; $content = stripslashes($_POST['content']); $fp = fopen($fn,"w") or die ("Error opening file in write mode!"); fputs($fp,$content); fclose($fp) or die ("Error closing file!"); ?>

Note how when the text area is generated, there is a space then
line return already present. How can I remove this so it is totally
blank?
(refer to the text.txt file generated - this is blank)


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

I’m using MAMP to test locally on the Mac.

How do change the line endings?


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

In Freeway, you select Document Preferences from the File menu, and
change it in the Output tab. If you are using a programmer’s text
editor, like TextMate or BBEdit, there will be a document-by-document
preference, usually in the main menu somewhere, to set the line-
endings for the currently open document.

Walter

On Jan 21, 2008, at 10:06 AM, dwn wrote:

I’m using MAMP to test locally on the Mac.

How do change the line endings?


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

I’ve changed in Freeway, (and checked with on line with text wrangler - on unix hosting line feed are unix LF) and the mysterious “space then line return” in still being created when the textarea is created from the PHP file (exactly same behavior)

There must be something in the PHP that is creating the space-line return in the textarea content when the code writes the page code.

Trouble is, I don’t know what it is thats doing it.


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