mport httplib, urllibimport base64, os.path
def imageToURL( aPath ):
# read the binary data of the picture
data = open(aPath, 'rb').read()
# encoded it to base64
encodedData = base64.encodestring( data )
headers = { "Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain",
}
params = urllib.urlencode({ u'fileName': os.path.split(aPath)[1],
u'data':encodedData})
conn = httplib.HTTPConnection( "yourURL.xxx" )
conn.request( "POST", "/uploaderFolder/image_uploader.php", params, headers )
response = conn.getresponse( )
# returns "True" or "False" if failed
print response.read( )
# status for debugging
print response.status
conn.close( )
if __name__ == "__main__":
imageToURL("yourImage.jpg")
Now the image uploader (image_uploader.php):
<?php
// Copyright (c) 2007 LEFEVRE Damien
// image_uploader.php implementation
// In this example a directory "images" needs to be present on the same directory where
// image_uploader.php is, with the necessary rights for the script to write data inside
if(isset($_POST['fileName'])){
$filename = $_POST['fileName'];
}
else{
// if not: just stop here
print "False";
die();
}
if(isset($_POST['data'])){
$encodedData = $_POST['data'];
$data = base64_decode($encodedData);
}
else{
// if not: just stop here
print "False";
die();
}
// full path for the image
$filepathname = 'images/'.$filename;
// write the file to the server into the images directory
$handle = fopen($filepathname, 'wb');
fputs($handle, $data, strlen($data));
fclose($handle);
// return
echo "True";
?>