Editing a .txt file Online & Reading
26-03-2010, 17:06
|
#1
|
|
Inactive
Join Date: Jan 2004
Location: Swansea
Posts: 1,376
|
Editing a .txt file Online & Reading
I'm looking for a simple way to have access to a .txt file online. I want to have a Text File in a folder on my FTP. Then open a HTML/PHP page & it will show me the contents of the file in ASCII format & give me the option to edit Text & Save the file (ie update the file in FTP).
Say file had Numbers, I could open a page, edit numbers & then save it.
I have a PHP script that posts from a FORM to Text File, but on page load the Form is empty (ie it does not Read the Text File).
Any free scripts that do this?
Cheers
|
|
|
26-03-2010, 20:33
|
#2
|
|
Inactive
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 46
Posts: 6,343
|
Re: Editing a .txt file Online & Reading
Gimme a min and I'll write a script for you...
---------- Post added at 19:33 ---------- Previous post was at 18:52 ----------
Here ya go.
Simple create the text file, modify the script to reflect the new filename, change the permissions so the webserver can read and write to it and you're done.
If you're not sure on the permissions thing, set it to 777 (all can read/write) - this should give the correct permissions to the file.
If you have shell access, do something like:
Code:
chown nobody:nobody textfile.txt
That should do it, if not find out which user Apache is running under by doing:
Code:
ps aux | grep httpd
OR
Code:
ps aux | grep apache
PHP Code:
<?php // Stop the addslashes() if (get_magic_quotes_gpc()) { $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST); while (list($key, $val) = each($process)) { foreach ($val as $k => $v) { unset($process[$key][$k]); if (is_array($v)) { $process[$key][stripslashes($k)] = $v; $process[] = &$process[$key][stripslashes($k)]; } else { $process[$key][stripslashes($k)] = stripslashes($v); } } } unset($process); } ?> <?php // Edit this if you need to, change the filename to whatever you need define ('TEXT_FILE', 'filename.txt'); define ('BYTES', 4096); // Expected max size of the file in bytes $message = '<br />Edit the file and click Save.<br />'; ?> <html> <head> <title>Text Editor</title> </head> <body> <?php if ($_POST['action'] == 'save') {
$fileWrite = fopen(TEXT_FILE, "w");
$text = $_POST['text']; fwrite($fileWrite, $text); $message = '<br />File saved, contents were: <pre>' . $text . '</pre><br />'; } if (is_resource($fileWrite)) { fclose($fileWrite); } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <?php $file = fopen(TEXT_FILE, "r"); echo '<textarea name="text" rows="12" cols="60">'; $contents = fread($file, BYTES); echo $contents; if (is_resource($file)) { fclose($file); } ?> </textarea> <br /> <input type="hidden" name="action" value="save" /> <input type="submit" value="Save" /> </form> <?php echo $message; ?> </body> </html>
Rather elegant I feel, will have to remember to keep this one for my own use.
|
|
|
28-03-2010, 12:04
|
#3
|
|
Inactive
Join Date: Jan 2004
Location: Swansea
Posts: 1,376
|
Re: Editing a .txt file Online & Reading
Simply AMAZING  It's PERFECT
Cheers
|
|
|
28-03-2010, 12:08
|
#4
|
|
-
Join Date: Jul 2003
Location: Poole, Dorset
Age: 40
Services: FreeSat+
Tivo
V-Box
VM 60MBit
Posts: 13,365
|
Re: Editing a .txt file Online & Reading
PHP Code:
if ($_POST['action'] == 'save')
might throw an error on some servers depending on how they're configured
PHP Code:
if ((isset($_POST['action'])) && ($_POST['action'] == 'save'))
may correct that, I haven't tested it so my syntax might be wrong, I'm a bit out of practice
|
|
|
28-03-2010, 12:14
|
#5
|
|
Inactive
Join Date: Dec 2007
Posts: 18,385
|
Re: Editing a .txt file Online & Reading
There are other options on the web for example
http://phpanywhere.net/
Although designed for php files it should work with any text based file
|
|
|
28-03-2010, 12:22
|
#6
|
|
Inactive
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 46
Posts: 6,343
|
Re: Editing a .txt file Online & Reading
Quote:
Originally Posted by Graham M
PHP Code:
if ($_POST['action'] == 'save')
might throw an error on some servers depending on how they're configured
PHP Code:
if ((isset($_POST['action'])) && ($_POST['action'] == 'save'))
may correct that, I haven't tested it so my syntax might be wrong, I'm a bit out of practice 
|
Aye maybe, should only throw a warning or notice though and the script would work anyway.
You're most welcome Jedi.
|
|
|
28-03-2010, 15:32
|
#7
|
|
-
Join Date: Jul 2003
Location: Poole, Dorset
Age: 40
Services: FreeSat+
Tivo
V-Box
VM 60MBit
Posts: 13,365
|
Re: Editing a .txt file Online & Reading
It definitely would work, I'm just being picky
|
|
|
28-03-2010, 15:43
|
#8
|
|
Inactive
Join Date: Jan 2004
Posts: 1,164
|
Re: Editing a .txt file Online & Reading
Better to use file_get_contents() too, then you don't need to guess at the maximum size of the file.
|
|
|
28-03-2010, 21:36
|
#9
|
|
Inactive
Join Date: Jun 2003
Location: Los Angeles, CA
Age: 46
Posts: 6,343
|
Re: Editing a .txt file Online & Reading
Quote:
Originally Posted by dev
Better to use file_get_contents() too, then you don't need to guess at the maximum size of the file.
|
Probably, I mostly look at things in C these days where everything has to be just right. :P
|
|
|
20-04-2010, 14:04
|
#10
|
|
Inactive
Join Date: Nov 2005
Location: Going sideways :)
Services: V+ | o2 BB
Posts: 522
|
Re: Editing a .txt file Online & Reading
I have also used this script for a friend to update a news area written in flash to read dynamic text on her website easily.
The script is excellent and when uploaded and tested it works great, but after a short period of time the txt file address needs typing into address bar, hit f5 and then the news area updates, but updates instantly when files are uploaded.
Any ideas?
|
|
|
20-04-2010, 22:28
|
#11
|
|
-
Join Date: Jul 2003
Location: Poole, Dorset
Age: 40
Services: FreeSat+
Tivo
V-Box
VM 60MBit
Posts: 13,365
|
Re: Editing a .txt file Online & Reading
It's probably the browser caching the text file, is the Flash animation using an Iframe sort of thing?
|
|
|
20-04-2010, 23:19
|
#12
|
|
Inactive
Join Date: Nov 2005
Location: Going sideways :)
Services: V+ | o2 BB
Posts: 522
|
Re: Editing a .txt file Online & Reading
I don't know what Iframe is, but I think you're right about caching, just what to do without having to refresh the text file.
|
|
|
21-04-2010, 12:55
|
#13
|
|
Inactive
Join Date: Apr 2010
Posts: 1
|
Re: Editing a .txt file Online & Reading
thanks for sharing this
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +1. The time now is 23:50.
|