7

Possible Duplicate:
Why are escape characters being added to the value of the hidden input

So, I have a file called Save.php.

It takes two things: a file, and the new contents.

You use it by sending a request like '/Resources/Save.php?file=/Resources/Data.json&contents={"Hey":"There"}'.

..but of course, encoding the url. :) I left it all unencoded for simplicity and readability.

The file works, but instead of the contents being..

{"Hey":"There"}

..I find..

{\"Hey\":\"There\"}

..which of course throws an error when trying to use JSON.parse when getting the JSON file later through XHR.

To save the contents, I just use..

file_put_contents($url, $contents);

What can I do to get rid of the backslashes?

Community
  • 1
  • 1
McKayla
  • 6,697
  • 5
  • 33
  • 46
  • Just before you save the contents to the file, what does the `$_GET['contents']` have? Does it have `{"Hey":"There"}` or `{\"Hey\":\"There\"}`? – Shef Jun 12 '11 at 21:00
  • They show up when using echo as well, but I don't use PHP an awful lot, it might add them too or something. xD – McKayla Jun 12 '11 at 21:01
  • Related: http://stackoverflow.com/questions/220437/magic-quotes-in-php – Orbling Jun 12 '11 at 21:02
  • It has already been answered, but gave you an answer tailored to your situation. – Shef Jun 12 '11 at 21:05
  • possible duplicate of [Why are escape characters being added to the value of the hidden input](http://stackoverflow.com/q/1038980/) – outis Jul 19 '12 at 02:40

6 Answers6

9

Turn magic_quotes off in PHP.ini.

SLaks
  • 837,282
  • 173
  • 1,862
  • 1,933
4

Looks like you have magic_quotes turned on.

If that is the case, either turn it off - Or use a runtime disabling function

Mick Hansen
  • 2,635
  • 17
  • 14
3

you probably have magic quotes enabled, only two things you can do. disable magic quotes in your php.ini or call stripslashes() on $_GET and $_POST globals.

FYI, use $_GET['contents'] as opposed to $contents; newer versions of php will not create the $contents var.

Ozzy
  • 9,867
  • 26
  • 90
  • 137
3

Try this:

file_put_contents($url, stripslashes($contents));
Shef
  • 43,457
  • 15
  • 77
  • 89
2

You should disable magic_quotes in your php.ini configuration file. However if this is not possible you can also use the stripslashes() function to get rid of the automatic escaping.

Seldaek
  • 39,307
  • 8
  • 94
  • 76
1

If you can not get magic quotes switched off for your server, then you need to check if it is switched on using get_magic_quotes_gpc() and if it is true, stripslashes().

Orbling
  • 20,025
  • 3
  • 50
  • 64