View Single Post
Join Date: Sep 2005
Posts: 1,051
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Python and PHP integration help: Incorrect Image URL decode?

 
0
  #2
Jul 24th, 2008
Originally Posted by ihateusernames View Post
Hello there everyone. For a summer project I'm doing, I have to use a phone to pass an image I've taken (on the phone) to a dedicated server I've made on a local ubuntu machine. For the upload code, I decided on python (seeing on how its easy to learn, and already on the phone), and for the server code I decided on PHP (that was also an easy decision). The problem I keep running into involves the image display: the data seems to be passed through to the PHP code allright, but when I try and display it on the server, this error pops up: "The image...cannot be displayed, because it contains errors. I think this has to do with incorrect decoding of the image data (or incorrect file writing), but so far I haven't been able to come up with a solution. Any help would be great!

Here is the upload code in python:
####
  1. import httplib, urllib
  2.  
  3.  
  4.  
  5. f = open(r"/home/cens/einstein.gif",'rb')
  6. #test image
  7.  
  8.  
  9. params = urllib.urlencode({'image': f.read() })
  10.  
  11. headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "image/gif"}
  12.  
  13. conn = httplib.HTTPConnection("pytest.no-ip.info")
  14.  
  15. conn.request("POST", "/upload.php", params, headers)
  16.  
  17. response = conn.getresponse()
###
And here is the PHP code that it connects with
###
...
  1. Code: ( text )
  2. $uploaddir = '/home/cens/www/uploads/';
  3. echo '<pre>';
  4. $uploadfile = $uploaddir . "new.gif";
  5. $imgcontent = urldecode($_POST['image']);
  6.  
  7. $fh = fopen($uploadfile, 'wb') or die("can't open file");
  8.  
  9. fwrite($fh, $imgcontent);
  10. fclose($fh);
##

On an additional note, I also wrote some HTML code that feeds directly into the PHP file, and the images that I recieve through this method are fine, and display without any problems (this is what led me to believe the problem lied in the encoding part in the first place).

Thanks for the help!
Andrew

How are you displaying the image? Could you post the code for that?
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote