Question

In my code I am having PHP write a variable into a text file.

$stringData = '<p class="blogcontent">' . $content . '</p>';
fwrite($fh, $stringData);

The variable $content includes this <img src="bpictures/blue.jpg" width="200" />

But when the variable is written in to the text file this is written into it.

<img src=\"bpictures/blue.jpg\" width=\"200\" />

And it causes the image not to work when returned into html. I have tried using echo stripslashes($content); but that doesn't work, Is there any way that I can write into the text file just the straight code that I have in the variable? I can't find an answer on Google.

How content is made.

<span class="addblog">Content (Include img tags from below):</span> <textarea cols="90" rows="50" name="bcontent">  </textarea> <br />

On submit.

$content = $_POST["bcontent"];

There is much more to the code but this is all that effects content for the most part.

Was it helpful?

Solution

The problem is that you have magic_quotes enabled.

If you can't disable them on the php.ini, you can use this function I found here on stackoverflow a while ago, to disable then at run time:

<?php
if (get_magic_quotes_gpc()) {
    function stripslashes_gpc(&$value)
    {
        $value = stripslashes($value);
    }
    array_walk_recursive($_GET, 'stripslashes_gpc');
    array_walk_recursive($_POST, 'stripslashes_gpc');
    array_walk_recursive($_COOKIE, 'stripslashes_gpc');
    array_walk_recursive($_REQUEST, 'stripslashes_gpc');
}
?>

Found the original post related to this subject, here!

There are other solutions present there, but this was the one I've used.


EDITED

From the above link a simple solution to deal with the $_POST:

if (get_magic_quotes_gpc()) {
  $content = stripslashes($_POST['bcontent']);
}else{
  $content = $_POST['bcontent'];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top