0

I have a raspberry pi running a lamp stack, an arduino and a camera hooked up. The end goal is that when my arduino takes a photo, it then writes an image to a php address which is then emailed.

Right now, I'm trying to get the image to get placed in the right place.

Here's my php snippet:

<?php
print_r($_FILES);
move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/images/mypic.jpg");
?>

My python code is 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 the image is going to get overwritten. That's not a problem. I can always add a timestamp later etc etc.

However, this gives me an error code. I'm a beginner at php and use python mainly for scientific computing so not sure if I'm passing the picture correctly. I know that the ip is correct as I can connect to it and it's all in network.

I have looked at this Python script send image to PHP but am still getting stuck.

EDIT: Upon further debugging:

print_r($_POST);

returns an empty array. Not sure why?

10
  • Always include errors you get Commented Oct 3, 2015 at 5:41
  • 1
    It actually is, at least we know it's trying to handle the request. 500 errors should be logged, try having a look in /var/logs/apache2/error.log or your equivialent (on 192.168.1.100). Commented Oct 3, 2015 at 5:43
  • 1
    It's the request that tries to execute accept_image.php in your web root (/var/www), but that file does not exist. Either change the web root to where you have the file or move the file into /var/www Commented Oct 3, 2015 at 5:56
  • 1
    you are trying to pass an image in post, but the php file you are trying to post to doesn't exist. In which file do you have the php snippet you pasted above? Commented Oct 3, 2015 at 5:58
  • 1
    /varr or /var? Can you open 192.168.1.100/accept_image.php in a browser (try to echo/var dump something)? Commented Oct 3, 2015 at 6:02

1 Answer 1

1

To have a file accessible to PHP in $_FILES, you must use HTML-form style encoding of files (multipart/form-data). This is different from a standard POST request including the content in the request body. This method is explained in http://php.net/manual/en/features.file-upload.post-method.php - the key here is:

PHP is capable of receiving file uploads from any RFC-1867 compliant browser.

What's you're trying to do is not sending it the RFC-1867 way, but a plain-old POST request.

So you have two options:

  1. Send the data from Python using multipart/form-data encoding. It shouldn't be too hard but requires some work on the Python side.

  2. Just grab the data on the PHP side not from $_FILES but by directly reading it from the POST body, like so:

.

$data = file_get_contents('php://input');
file_put_contents("/var/www/images/mypic.jpg", $data);

It's more of a memory hog on the PHP side, and means you need to do some validation that you actually got the data, but is quite simpler.

To clarify, in PHP $_POST is only populated when the Content-type request header is multipart/form-data or application/x-www-form-urlencoded (and of course the data is encoded in the proper way).

If you get a POST request with anything else in the body, you can read it directly by reading from the php://input stream, and you're responsible for handling / decoding / validating it.

Sign up to request clarification or add additional context in comments.

2 Comments

this kind of works. But now when it moves the pic, it says it can't be displayed because it has errors
actually, I think my Rcurl was broken. Doing it through python worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.