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,879
  • 5
  • 36
  • 48
  • 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
  • 868,454
  • 176
  • 1,908
  • 1,964
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,685
  • 18
  • 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
  • 10,285
  • 26
  • 94
  • 138
3

Try this:

file_put_contents($url, stripslashes($contents));
Shef
  • 44,808
  • 15
  • 79
  • 90
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
  • 40,986
  • 9
  • 97
  • 77
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,413
  • 3
  • 53
  • 64