Log in

View Full Version : Edit multiple txt files from 1 php page



missodessa
07-03-2010, 08:53 AM
I am trying to edit multiple text files from a single php code/page. I want there to be a box for each text file I want to edit so they can all be updated on the same page at the same time. I have a code that works well for what I want to do. However this only has one form to edit one text file and Im not to familar with php to edit it and add another box and was wondering if someone else could help me fix it?

The reason I am doing this is because I have a flash file that has certain areas that load external text files. I would like to be able to edit all these text files so that I can easily update my flash from one single page if you catch my drift.

here is the code I am using and a link to the page its on so you can see it in action http://missodessa.net/php/editsomethingtxt.php

ANy help would be greatly appreciated :-)





<?php
if (isset($_POST['submit'])) {

$myFile = "something.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = stripslashes($_POST['sf']);
fwrite($fh, $stringData);
fclose($fh);

header('Location: http://missodessa.net/php/editsomethingtxt.php?a=done');

}
?>

Inside body:
<form action="" method="post">
<textarea name="sf" cols="40" rows="6">
<?php
$myFile = "something.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;
?></textarea>
<br />
<input type="submit" name="submit" value="Edit" />
</form>

<?php
if ($_GET['a'] == 'done') {
echo 'The file was saved and now it says:<br /><br />';

$myFile = "something.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;

}
?>

fileserverdirect
07-05-2010, 12:27 AM
First make sure the text file is chmod-ed to 777, (do this you your ftp client)
If that doesn't work, wherever your code is this:


$myFile = "something.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;

replace with


$theData = file_get_contents("something.txt");
echo $theData;

and then replace the submiting code with this:


if (isset($_POST['submit'])) {

$stringData = stripslashes($_POST['sf']);
file_put_contents("something.txt", $stringData);

header('Location: http://missodessa.net/php/editsomethingtxt.php?a=done');

}


*Untested