0

Here's a stupid question, don't laugh, I've been trying to figure this out but no luck, I guess I'm too tired. So there's an application sending data (image file as string) to a php script, sending it with XMLHttpRequest.send(data). My question is how do I access this string in PHP to save it to a file?

EDIT $_POST, $_GET and $_FILES are all empty

slash197
  • 9,028
  • 6
  • 41
  • 70
  • $_REQUEST?(http://php.net/manual/en/reserved.variables.request.php).If all of the server variables are empty, the problem is likely in your javascript sending stuff. A snippet would help. – Visionary Software Solutions Apr 11 '12 at 19:03

1 Answers1

1

First: Make sure you are sending a POST request (when you call the open method)

Second: Since it looks like you are sending raw data, set an appropriate content type (with setRequestHeader)

Third: You should be able to retrieve the data with $HTTP_RAW_POST_DATA or file_get_contents("php://input");.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I got it with `php://input`, thank you very much. Any idea how to parse the string if it starts with `data:image/png;base64...`, after this there are a bunch of random characters which I think it's the image itself. This string was generated with html5's `canvas.toDataURL()` I believe. – slash197 Apr 11 '12 at 19:18
  • @slash197 you pass the not-so-random characters to `base64_decode()`, then save it to a file. – Umbrella Apr 11 '12 at 19:25