PDA

View Full Version : how do i modify existing .txt files with php?



gusblake
08-21-2005, 09:44 PM
i need to add the value of a text box to a txt file on my server which will then be included in a textarea on the page. how will i do this (i know how to do the include bit)? thanks

Twey
08-23-2005, 11:28 AM
<?php
$fn = "file.txt";
$file = fopen($fn, "a+");
$size = filesize($fn);

if($_POST['addition']) fwrite($file, $_POST['addition']);

$text = fread($file, $size);
fclose($file);
?>
<form action="<?=$PHP_SELF?>" method="post">
<textarea><?=$text?></textarea><br/>
<input type="text" name="addition"/>
<input type="submit"/>
</form>
That ought to work.

June
02-20-2006, 09:44 PM
Hi Twey,

Nice code. How would it need to be changed so that instead of 'adding' text to the .txt file, it actually 'replaced' the text in the .txt file?

kind regards,

June

Twey
02-20-2006, 09:46 PM
Change the "a+" to "w+" in fopen (http://uk.php.net/fopen)().

June
02-20-2006, 10:10 PM
Change the "a+" to "w+" in fopen (http://uk.php.net/fopen)().

Produces the error message:

Warning: fread(): Length parameter must be greater than 0

June
02-20-2006, 10:57 PM
r+ seems to work without an error.

Your link says the following:

'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

I would like to get w+ working.

Twey
02-21-2006, 04:43 PM
Remove the line with the fread() call -- as you're removing all the contents of the file, it doesn't make sense to try to read in said contents.

June
02-22-2006, 01:31 AM
Hi Twey, firstly many thanks for your help.

Here's what I'm trying to do... use the code as a basic 'text file editor'. The text file is then used as an 'include' in a single webpage. Here's the code.


<?php
$fn = "textfile.txt";
$file = fopen($fn, "w+");
$size = filesize($fn);

if($_POST['update']) fwrite($file, $_POST['update']);

//$text = fread($file, $size);
fclose($file);
?>
<form action="<?=$PHP_SELF?>" method="post">
<textarea Name="update" cols="50" rows="10"><?=$text?></textarea><br/>

<input type="submit" value="Update"/>

</form>


By putting // in front of

$text = fread($file, $size);

it now doesn't load the text into the textarea from the text file (which it would need to do to edit the text).

It needs to load the text into the textarea, and be able to replace the content in the text file. At the moment it does one or the other.

If we could then put a WYSIWYG editor on the textarea such as
http://www.dynamicdrive.com/dynamicindex16/richtexteditor/index.htm

Then it would make a really fantastic basic CMS.

Also on submit perhaps the form could reload the current form page (so showing the updated text in the textarea), or go to another page?

What do you think?

Pixelcode
02-22-2006, 08:31 AM
<?
if($_POST['Submit']){
$open = fopen("textfile.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />";
echo "File:<br />";
$file = file("textfile.txt");
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("textfile.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
}
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>


:)

June
02-22-2006, 01:38 PM
<?
if($_POST['Submit']){
$open = fopen("textfile.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />";
echo "File:<br />";
$file = file("textfile.txt");
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("textfile.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
}
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>


:)


That's great!

1. On the results page showing what has been submitted (not the text file) it needs a couple of links under the text (because re-entering the url is showing the results and not the form). So to the above, how would I add?


<p><a href="./livepage.php">click here to view the live updated webpage</a></p>
<p><a href="./mainadminpage.php">click here to view the admin menu</a></p>

2. Needs to include a free WYSIWYG editor on the text area. Anyone able to do that?

And that would be it to make a great little CMS.

Thanks,

June

Pixelcode
02-22-2006, 01:57 PM
<?
if($_POST['Submit']){
$open = fopen("textfile.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />";
echo "File:<br />";
$file = file("textfile.txt");
foreach($file as $text) {
echo $text."<br />";
}
echo "<p><a href=\"./livepage.php\">click here to view the live updated webpage</a></p>";
echo "<p><a href=\"./mainadminpage.php\">click here to view the admin menu</a></p>";
}else{
$file = file("textfile.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
}
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>
:)

June
02-22-2006, 02:22 PM
<?
if($_POST['Submit']){
$open = fopen("textfile.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />";
echo "File:<br />";
$file = file("textfile.txt");
foreach($file as $text) {
echo $text."<br />";
}
echo "<p><a href=\"./livepage.php\">click here to view the live updated webpage</a></p>";
echo "<p><a href=\"./mainadminpage.php\">click here to view the admin menu</a></p>";
}else{
$file = file("textfile.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
}
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>
:)

That was quick Pixelcode you beat me to it ;) I came up with:



echo $text."<br /> <p><a href=\"./livepage.php\">click here to view the live webpage</a></p>
<p><a href=\"./adminpage.php\">click here to view the admin menu</a></p>";


Now only to add the WYSIWYG editor.

The instructions are here, but I get stuck on steps 3 and 4.

http://www.dynamicdrive.com/dynamicindex16/richtexteditor/index.htm

Do you know how to drop the WYSIWYG code into the CMS form page?

Many thanks!

June

nyem
06-28-2006, 12:26 PM
This is exactly what I needed as well, thanks for sharing the code. What would I need to add to update two different files, (textfile.txt and textfile1.txt) with different content in each?

djr33
06-28-2006, 12:34 PM
Make two different pages, changing all instances of "textfile.txt" to "textfile1.txt".

OR, create some way to control it, and you could use a variable storing that value, and you could use the variable in the script instead.
Or are you asking to do them at the same time? I'd say just cut/paste the code twice and change the instances of "textfile.txt" to "textfile1.txt". There may be a shorter way, but... no need.

nyem
06-28-2006, 02:28 PM
Thanks. I'll just create two pages.

I've also found that if I try to add any text in quotes e.g "http://www.whatever.com" this gets altered to
\"http://www.whatever.com\" and then if I save the file again
as \\\"http://www.whatever.com\\\" and so on.

Is there any way to prevent this happening?

Twey
06-28-2006, 02:34 PM
Call stripslashes() on the data before writing it.

djr33
06-29-2006, 07:00 AM
The slashes are added to prevent the quotes from hurting the code, where a quote would end the string, and the rest would end up being treated as php code... and that would be ugly.
The effect is that of addslahes(), and to undo, yes, use what Twey said.

Always alternate those two.

nyem
06-29-2006, 08:46 AM
Thanks, works fine.

rickarro
04-20-2008, 03:54 AM
Let me start by saying this little script is great, just what i was looking for. I got it working perfectly in my test environment, but when i put it in my live area, I can't update the txt file. I've gone over it multiple times and compared it to my test pc, but nothing is out of place. The only change i made was I had to add PHP to <?, and i changed the names of the txt file in all three places. It works fine, i connect to the txt file and read it, but It won't update. I made sure it has the w+. Not sure whats going on. Any help is much appreciated.

<?php
if($_POST['Submit']){
$open = fopen("b1.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />";
echo "File:<br />";
$file = file("b1.txt");
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("b1.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
}
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>

rickarro
04-20-2008, 04:28 AM
I found my answer reading other posts. I failed to check the permissions on the txt files. All is good now. Sorry about that.

But....if anyone can show me how to modify this script to pull the text from a MySQL database instead of a txt file that would be great. I started with the txt file because it was something I could do, but my next step will be attempting to pull the data from a database. Just thought I'd run it by ya'll.

Thanks.