Page 1 of 1
escaping in fwrite() and fopen()
Posted: Thu Oct 07, 2004 9:58 pm
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.

Posted: Fri Oct 08, 2004 12:37 am
by feyd
I'd guess you have magic quotes on... so .... [php_man]stripslashes[/php_man]()
Posted: Fri Oct 08, 2004 3:09 am
by twigletmac
If you have access to the php.ini you can also turn magic_quotes off permanently (which is generally recommended).
Mac
Posted: Fri Oct 08, 2004 2:57 pm
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>
?>
Posted: Fri Oct 08, 2004 3:08 pm
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.
Posted: Fri Oct 08, 2004 6:24 pm
by Daisy Cutter
thanks a million feyd, it works like a charm.