Python to PHP on server send image -
i have raspberry pi running lamp stack, arduino , camera hooked up. end goal when arduino takes photo, writes image php address emailed.
right now, i'm trying image placed in right place.
here's php snippet:
<?php print_r($_files); move_uploaded_file($_files["file"]["tmp_name"], "/var/www/images/mypic.jpg"); ?>
my python code doing:
import requests r = requests.get('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png') r2 = requests.post('http://192.168.1.100/accept_image.php', data = r.content)
i realize image going overwritten. that's not problem. can add timestamp later etc etc.
however, gives me error code. i'm beginner @ php , use python scientific computing not sure if i'm passing picture correctly. know ip correct can connect , it's in network.
i have looked @ python script send image php still getting stuck.
edit: upon further debugging:
print_r($_post);
returns empty array. not sure why?
to have file accessible php in $_files
, must use html-form style encoding of files (multipart/form-data
). different standard post request including content in request body. method explained in http://php.net/manual/en/features.file-upload.post-method.php - key here is:
php capable of receiving file uploads rfc-1867 compliant browser.
what's you're trying not sending rfc-1867 way, plain-old post request.
so have 2 options:
send data python using
multipart/form-data
encoding. shouldn't hard requires work on python side.just grab data on php side not $_files directly reading post body, so:
.
$data = file_get_contents('php://input'); file_put_contents("/var/www/images/mypic.jpg", $data);
it's more of memory hog on php side, , means need validation got data, quite simpler.
to clarify, in php $_post populated when content-type request header multipart/form-data
or application/x-www-form-urlencoded
(and of course data encoded in proper way).
if post request else in body, can read directly reading php://input
stream, , you're responsible handling / decoding / validating it.
Comments
Post a Comment