User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,929 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,717 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 252 | Replies: 1
Reply
Join Date: Jul 2008
Posts: 1
Reputation: ihateusernames is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ihateusernames ihateusernames is offline Offline
Newbie Poster

Python and PHP integration help: Incorrect Image URL decode?

  #1  
28 Days Ago
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:
####
import httplib, urllib



f = open(r"/home/cens/einstein.gif",'rb')
#test image


params = urllib.urlencode({'image': f.read() })

headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "image/gif"}

conn = httplib.HTTPConnection("pytest.no-ip.info")

conn.request("POST", "/upload.php", params, headers)

response = conn.getresponse()
###
And here is the PHP code that it connects with
###
...
Code: ( text )
      $uploaddir = '/home/cens/www/uploads/';
      echo '<pre>';
      $uploadfile = $uploaddir . "new.gif";
      $imgcontent = urldecode($_POST['image']);

      $fh = fopen($uploadfile, 'wb') or die("can't open file");

      fwrite($fh, $imgcontent);
      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 : 28 Days Ago at 2:20 am. Reason: Code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2005
Posts: 641
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 5
Solved Threads: 38
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

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

  #2  
28 Days Ago
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:
####
import httplib, urllib



f = open(r"/home/cens/einstein.gif",'rb')
#test image


params = urllib.urlencode({'image': f.read() })

headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "image/gif"}

conn = httplib.HTTPConnection("pytest.no-ip.info")

conn.request("POST", "/upload.php", params, headers)

response = conn.getresponse()
###
And here is the PHP code that it connects with
###
...
Code: ( text )
      $uploaddir = '/home/cens/www/uploads/';
      echo '<pre>';
      $uploadfile = $uploaddir . "new.gif";
      $imgcontent = urldecode($_POST['image']);

      $fh = fopen($uploadfile, 'wb') or die("can't open file");

      fwrite($fh, $imgcontent);
      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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 8:27 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC