473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. ####
  2. import httplib, urllib
  3.  
  4.  
  5.  
  6. f = open(r"/home/cens/einstein.gif",'rb')
  7.  #test image
  8.  
  9.  
  10. params = urllib.urlencode({'image': f.read() })
  11.  
  12. headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "image/gif"}
  13.  
  14. conn = httplib.HTTPConnection("pytest.no-ip.info")
  15.  
  16. conn.request("POST", "/upload.php", params, headers)
  17.  
  18. response = conn.getresponse()
  19.  
  20. ###
  21.  
And here is the PHP code that it connects with
###
...
[PHP]$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);[/PHP]
##

EDIT: 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!
Jul 23 '08 #1
1 1955
Another thought: would it easier to simply upload to a table in a php database instead of messing around with an upload directory?
Jul 23 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 407 open ( +1) / 3484 closed ( +5) / 3891 total ( +6) Bugs : 936 open ( +5) / 6363 closed (+14) / 7299 total (+19) RFE : 246 open...
0
by: Independent | last post by:
Python programmers may find the application to decoding an encrypted map image format known as Memory Map to produce a standard PNG image file interesting. Someone obviously very well versed in...
0
by: james.duckworthy | last post by:
Python programmers may find the application to decoding an encrypted map image format known as Memory Map to produce a standard PNG image file interesting. Someone obviously very well versed in...
1
by: anonymous | last post by:
1 Objective to write little programs to help me learn German. See code after numbered comments. //Thanks in advance for any direction or suggestions. tk 2 Want keyboard answer input, for...
2
by: Mark Tolonen | last post by:
I don't understand the behavior of the interpreter in Python 3.0. I am working at a command prompt in Windows (US English), which has a terminal encoding of cp437. In Python 2.5: Python 2.5...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.