2

Good day. Can somebody help me. My task is to create an python script (client side) that send image to php (server side).

NOTE: The python script is run in different raspberry pi, and the php server only receive the image via internet.

Achievement: I can now send a text data from my client to server.

Problem: My big problem is how can I send the image?

Any comments and suggestion is very appreciated. Thank You.

My Python Script:

import urllib2
from urllib import urlencode 

# 192.168.5.149 is the ip address of server
url = "http://192.168.5.149/server/server.php"
data = {'test':'OK'}

encoded_data = urlencode(data)

website = urllib2.urlopen(url, encoded_data)
print website.read()

My PHP script:

<?php
echo $_POST['test'];
?>

When I run the python script, I got "ok" as send by PHP server. That means, the connection is successful.

EDITED

Python client:

import requests
url = 'http://messi-fan.org/post'
files = {'file': open('image.png', 'rb')}
r = requests.post(url, files=files)

PHP server:

<?php
$file_path = "C:\\xampp\htdocs\server\php\\";

$file_path = $file_path.basename( $_FILES['file']['name']);
?>
1
  • hope this helps link Commented Aug 28, 2014 at 8:32

1 Answer 1

10

You can use requests module for this. it is very easy to use

import requests
url = 'http://messi-fan.org/post'
files = {'file': open('image.png', 'rb')}
r = requests.post(url, files=files)

and in PHP

<?php
print_r($_FILES);
move_uploaded_file($_FILES["file"]["tmp_name"],$_FILES["file"]["name"]);
Sign up to request clarification or add additional context in comments.

4 Comments

Requests is a much nicer library than urllib but it's not part of Python's standard library and must be separately installed using pip or your system's package manager. If this is not an option then website = urllib2.urlopen(url, urlencode(files)) will work too.
Thank you for your time. In your suggestion code, what "&_FILES" means? Is that the image? How can i save the image to my local directory? Ex:$file_path = "C:\\xampp\htdocs\server\php\\";
$_FILES variable contain the uploaded image file. you can save the uploaded file anywhere in your server
How can I save this $_FILES to my local drive? I have some code applied above but i don't know what's wrong.please help me.

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.