.txt include question.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
delveart
Forum Newbie
Posts: 6
Joined: Thu Sep 18, 2008 9:20 am
Location: Springfield

.txt include question.

Post by delveart »

I recently built a website that is update-able by the customer. I created a form that writes to a .txt file. The .txt file is then included on the home page, its pretty basic. Every time the site is updated by the customer and they use any of the following punctuation symbols ' " , it adds a / symbol. What I need to know is how to fix my code so that it the punctuation shows up correctly. I am a beginner with this, so assume that I know the least. I am going to set this up in a database when I have time to learn how, but for now I need this to work.

Here is the code from the form page.

<html>
<head>
<title>DLRA News Editor</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../dlra.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
body {
background-color: #333333;
}
-->
</style>
<link href="dlraadmin.css" rel="stylesheet" type="text/css">
</head>

<body>
<?php include("updates.php"); ?>
<h1 align="center">News Update</h1>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="top"><?php
$saving = $_REQUEST['saving'];
if ($saving == 1) {
$data = $_POST['data'];
$file = "news.txt";

$fp = fopen($file, "w") or die("Couldn't open $file for writing!");
fwrite($fp, $news) or die("Couldn't write values to file!");

fclose($fp);
echo "Saved to $file successfully!";
}
?>

<form align="center" name="form1" method="post" action="newseditor.php?saving=1">
<textarea name="news" cols="80" rows="20">
<?php
$file = "news.txt";
if (!empty($file)) {
$file = file_get_contents("$file");
echo $file;
}
?>
</textarea>
<br>
<br>
<input type="submit" value="Update News">
</form></td>
</tr>
</table>
<div align="center">
<p><a href="http://www.dlra.net" target="_blank" class="style1"><u>View Update</u>
</a></p>
</div>


</body>
</html>
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: .txt include question.

Post by Jade »

If you do an addslashes() to the info the post, you need to do a stripslashes() to remove them again. Or, if you're encoding them you need to do html_entity_decode to display them properly again.
User avatar
delveart
Forum Newbie
Posts: 6
Joined: Thu Sep 18, 2008 9:20 am
Location: Springfield

Re: .txt include question.

Post by delveart »

do I put the strip slashes on the page that has the include?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: .txt include question.

Post by Jade »

Yes, wherever you're displaying the file contents in the include.
User avatar
delveart
Forum Newbie
Posts: 6
Joined: Thu Sep 18, 2008 9:20 am
Location: Springfield

Re: .txt include question.

Post by delveart »

I successfully got rid of the slashes in the form but I am still getting them in the website content.


Here is my original include.

<?php $file = 'admin/news.txt';
$fp = fopen($file,'r');
$info = fread($fp, filesize($file));
$info = nl2br($info);
echo $info; ?>

Here is the change I made

<?php $file = 'admin/news.txt';
$fp = fopen($file,'r');
$info = fread($fp, filesize($file));
$info = nl2br($info);
echo stripslashes("admin/news.txt");
echo $info; ?>

What am I doing wrong?
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: .txt include question.

Post by Sindarin »

echo stripslashes("admin/news.txt");
wait, shouldn't it be,
stripslashes($info);
User avatar
delveart
Forum Newbie
Posts: 6
Joined: Thu Sep 18, 2008 9:20 am
Location: Springfield

Re: .txt include question.

Post by delveart »

ha it worked. Thanks for both of your help. Here is what the final include looked like.

<?php $file = 'admin/news.txt';
$fp = fopen($file,'r');
$info = fread($fp, filesize($file));
$info = nl2br($info);
$info = stripslashes($info);

echo $info; ?>

Here is the site

http://www.dlra.net

Thanks a ton
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: .txt include question.

Post by Jade »

Yes, it should be

Code: Select all

 
<?php
echo stripslashes($info);
?>
 
Post Reply