Cable Forum

Cable Forum (https://www.cableforum.uk/board/index.php)
-   Internet Discussion (https://www.cableforum.uk/board/forumdisplay.php?f=25)
-   -   Editing a .txt file Online & Reading (https://www.cableforum.uk/board/showthread.php?t=33663184)

JediMaster 26-03-2010 17:06

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

Richard M 26-03-2010 20:33

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($fileBYTES);
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.

JediMaster 28-03-2010 12:04

Re: Editing a .txt file Online & Reading
 
Simply AMAZING :D It's PERFECT :)

Cheers

Graham M 28-03-2010 12:08

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 :p:

Kymmy 28-03-2010 12:14

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

Richard M 28-03-2010 12:22

Re: Editing a .txt file Online & Reading
 
Quote:

Originally Posted by Graham M (Post 34989194)
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 :p:

Aye maybe, should only throw a warning or notice though and the script would work anyway.

You're most welcome Jedi.

Graham M 28-03-2010 15:32

Re: Editing a .txt file Online & Reading
 
It definitely would work, I'm just being picky ;)

dev 28-03-2010 15:43

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.

Richard M 28-03-2010 21:36

Re: Editing a .txt file Online & Reading
 
Quote:

Originally Posted by dev (Post 34989321)
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

mr_bo 20-04-2010 14:04

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?

Graham M 20-04-2010 22:28

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?

mr_bo 20-04-2010 23:19

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.

marrydavidson101 21-04-2010 12:55

Re: Editing a .txt file Online & Reading
 
thanks for sharing this


All times are GMT +1. The time now is 22:28.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
All Posts and Content are © Cable Forum