Python and PHP integration help: Incorrect Image URL decode?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zandrew
    New Member
    • Jul 2008
    • 2

    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:
    [code=python]
    ####
    import httplib, urllib



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


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

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

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

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

    response = conn.getrespons e()

    ###
    [/code]
    And here is the PHP code that it connects with
    ###
    ...
    [PHP]$uploaddir = '/home/cens/www/uploads/';

    echo '<pre>';

    $uploadfile = $uploaddir . "new.gif";
    $imgcontent = urldecode($_POS T['image']);

    $fh = fopen($uploadfi le, '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!
    Last edited by Atli; Jul 24 '08, 12:26 AM. Reason: Added [code] tags
  • zandrew
    New Member
    • Jul 2008
    • 2

    #2
    Another thought: would it easier to simply upload to a table in a php database instead of messing around with an upload directory?

    Comment

    Working...