Dave
Dave

Reputation: 477

How to save a POST jpeg to a server using PHP that isn't coming from a html form?

I have to save a jpeg that is sent to my server as a POST with this information: "The image will be uploaded to the URL by using the POST method. The field "data_image" will contain the image/jpeg data and the "data_device_id" field will contain the device id of this device."

I provide the URL to my server's PHP script.

I don't think I can use this: $_FILES from http://www.w3schools.com/php/php_file_upload.asp as it is not coming from a form therefore I have no "name" of the form. I searched all over but I can't figure out how to take this jpeg data and save it to a file on my server using a PHP script. I know how to use $_FILES from forms but this I'm stumped.

Upvotes: 0

Views: 1460

Answers (2)

Alex W
Alex W

Reputation: 38193

You can access variables that are sent to your PHP script using the $_POST array:

echo "Device ID: " . $_POST['data_device_id'];

Upvotes: 0

sprain
sprain

Reputation: 7672

Sounds as if you just have to save image data to a file:

file_put_contents('yourFilename.jpg', $_POST['data_image']);

However, it's possible that $_POST['data_image'] is containing more data, maybe as an array or in some other kind of data format (JSON, XML), You can find out by having a look at the data:

var_dump($_POST);

You could also save this output to a file or have it sent to you by email if the data can only be sent from an external source.

Upvotes: 1

Related Questions