[SOLVED] escaping in fwrite() and fopen()

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
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

escaping in fwrite() and fopen()

Post by Daisy Cutter »

how can I stop fwrite and fopen from escaping charachters, or print them without escaping? me and a friend are working on a programming project and I set up a "notepad" type program for us to leave messages/code for eachother but we can't copy and paste code because quotes and slashes get escaped. :twisted:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd guess you have magic quotes on... so .... [php_man]stripslashes[/php_man]()
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If you have access to the php.ini you can also turn magic_quotes off permanently (which is generally recommended).

Mac
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

Post by Daisy Cutter »

sorry for being so dense but could anyone tell me how I would apply stripslashes to the output of this? Or how can I disable magicquotes via .htaccess?

Code: Select all

<form id="fwrite" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<!-- open: <input type="text" name="filename" size="25">.txt<br /> -->

<?php

$filename = "cody.txt";
$somecontent = $_POST['writer'];

if ($_POST['overwrite'] == true) {
$wr = 'w'; }
else{
$wr = 'a'; }

if (is_writable($filename)) {
   if (!$handle = fopen($filename, $wr)) {
         echo "Cannot open file ($filename)";
         exit; }
   // Write $somecontent to our opened file.
   if (fwrite($handle, $somecontent) === FALSE) {
       echo "Cannot write to file ($filename)";
       exit; }
   echo "Success";
   fclose($handle); }
   else {
   echo "The file $filename is not writable";
}
?>
<br />
<textarea name="writer" rows="25" cols="75">
<?php
include $filename;
?>
</textarea><br />
<input type="checkbox" name="overwrite" value="x" />overwrite?<br />
<input type="submit" value="save" />
</form>
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

first, you check if magic quotes is on using [php_man]get_magic_quotes_gpc[/php_man](). if it is, use [php_man]stripslashes[/php_man]() on the value coming in.

this all happens on line 7.
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

Post by Daisy Cutter »

thanks a million feyd, it works like a charm.
Post Reply