943,794 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2365
  • PHP RSS
Jul 23rd, 2008
0

Python and PHP integration help: Incorrect Image URL decode?

Expand 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:
####
PHP Syntax (Toggle Plain Text)
  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
###
...
PHP Syntax (Toggle Plain Text)
  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
Last edited by Tekmaven; Jul 24th, 2008 at 3:20 am. Reason: Code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ihateusernames is offline Offline
1 posts
since Jul 2008
Jul 24th, 2008
0

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

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:
####
PHP Syntax (Toggle Plain Text)
  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
###
...
PHP Syntax (Toggle Plain Text)
  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?
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: E-Mail validation
Next Thread in PHP Forum Timeline: Use same part of a variable for two different things





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC