Jump to content

Recommended Posts

Hi, I'm fairly new to php, and I am having a problem with updating a txt file using php.  We are building some template websites, and we want customers to be able to update the text in some areas.  The text in those areas is read from a txt file that can be changed using a text area and submit form in the admin area.

 

The problem is that for certain characters, such as quotation (") marks, php puts a slash (\) in front of the character.  I understand that this is part of the syntax of php, but how do I get it to echo exactly what is put in the text area, and not add a slash before syntax characters like quotes?

 

Here is my code.  "hometext.txt" is the text file that is being written to and read from.

<?
if($_POST['Submit']){
$open = fopen("hometext.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />"; 
echo "File:<br />";
$file = file("hometext.txt");
foreach($file as $text) {
echo $text."<br />";
}
echo "<p><a href=\"../indexDynamic.php\">click here to view your changes live</a></p>";
echo "<p><a href=\"../upload\">click here to make more changes</a></p>";
}else{
$file = file("hometext.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>";
}
?> 

 

Any help would be much appreciated!  If I should use a different method other than reading from a txt file, let me know.

Link to comment
https://forums.phpfreaks.com/topic/139728-update-txt-file-with-php/
Share on other sites

First, it's much better to weave in and out of the php. So instead of echoing the html, just do this:

 

} ?>

 

<p><a href="../indexDynamic.php">click here to view your changes live</a></p>

<p><a href="../upload">click here to make more changes</a></p>

 

<?php }else{

 

------------

 

I think you will have to use stripslashes($contents); for your problem.

 

Hope that helps.

First, it's much better to weave in and out of the php. So instead of echoing the html, just do this:

 

} ?>

 

<p><a href="../indexDynamic.php">click here to view your changes live</a></p>

<p><a href="../upload">click here to make more changes</a></p>

 

<?php }else{

 

------------

 

I think you will have to use stripslashes($contents); for your problem.

 

Hope that helps.

 

Weaving in and out makes code a huge mess. I would use echo '' with single quotes and not escape the double quotes and remove the \n's from the code, but yea.

 

I actually tend to store my output in a string then display it after processing. But yea, I shutter at the weaving php tags....yuck. And as far as performance, they are about the same.

 

<?php
if(isset($_POST['Submit'])){
$open = fopen("hometext.txt","w+");
$text = stripslashes($_POST['update']);
fwrite($open, $text);
fclose($open);

echo 'File updated.<br />';
echo 'File:<br />';

$file = file("hometext.txt");
foreach($file as $text) {
	echo $text."<br />";
}

echo '<p><a href="../indexDynamic.php">click here to view your changes live</a></p>';
echo '<p><a href="../upload">click here to make more changes</a></p>';
}else{
$file = file("hometext.txt");
echo '<form action="'.$_SERVER{'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" /></form>';
}
?> 

 

Fixed some outdated issues, first added the stripslashes then changed $PHP_SELF to be $_SERVER['PHP_SELF'] then for the heck of it changed the echos to ' instead of " not to mention I properly indented your code. I would get in the habit of properly indenting code. It makes debugging much easier.

I do

First, it's much better to weave in and out of the php. So instead of echoing the html, just do this:

 

} ?>

 

<p><a href="../indexDynamic.php">click here to view your changes live</a></p>

<p><a href="../upload">click here to make more changes</a></p>

 

<?php }else{

 

------------

 

I think you will have to use stripslashes($contents); for your problem.

 

Hope that helps.

 

Weaving in and out makes code a huge mess. I would use echo '' with single quotes and not escape the double quotes and remove the \n's from the code, but yea.

 

I actually tend to store my output in a string then display it after processing. But yea, I shutter at the weaving php tags....yuck. And as far as performance, they are about the same.

 

<?php
if(isset($_POST['Submit'])){
$open = fopen("hometext.txt","w+");
$text = stripslashes($_POST['update']);
fwrite($open, $text);
fclose($open);

echo 'File updated.<br />';
echo 'File:<br />';

$file = file("hometext.txt");
foreach($file as $text) {
	echo $text."<br />";
}

echo '<p><a href="../indexDynamic.php">click here to view your changes live</a></p>';
echo '<p><a href="../upload">click here to make more changes</a></p>';
}else{
$file = file("hometext.txt");
echo '<form action="'.$_SERVER{'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" /></form>';
}
?> 

 

Fixed some outdated issues, first added the stripslashes then changed $PHP_SELF to be $_SERVER['PHP_SELF'] then for the heck of it changed the echos to ' instead of " not to mention I properly indented your code. I would get in the habit of properly indenting code. It makes debugging much easier.

 

Sorry, yeh, I should have realized. I think it depends how html heavy the page is that you are coding. Plus, no way would I use echo all the time while using an ide - hardly any ide treats the echoed html like html.

 

Each to their own I guess.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.