Python Framework for Symbian Series 60 phones

Thursday 15 February 2007

Upload file to URL with multipart/form-data

First of all make sure to download HTTPFileUploader and install in as a library.

Client source:

import os
from HTTPFileUploader import * # uploader class
 
# path of the file to download
filePath = "001.jpg"
 
# new HTTPFileUploader instance
uploader = HTTPFileUploader('yourServer.xxx', port = 80) # port optional if 80
 
# set page
uploader.setPage('/uploaderFolder/file_uploader.php')
 
# Add fields in HTTP: ex file name for or past example
uploader.setField("fileName", os.path.split(filePath)[1])
 
# upload file - returns True or False.
if not uploader.uploadFile(filePath, "picture"):
    print uploader.getResult()

Server source (file_uploader.php):

<?php
# Copyright (c) 2007 LEFEVRE Damien
# file_uploader.php implementation
 
// In this example a directory "images" needs to be present on the same directory where
// image_uploader.php is, with the necessary rights for the script to write data inside
$content_dir = 'images/';
$filename = Null;
 
if(isset($_POST['fileName'])){
    $filename = $_POST['fileName'];
}
 
if(isset($_FILES['picture'])){
    if ($filename == Null){
        $filename = $_FILES['picture']['name'];
    }
    if( !move_uploaded_file($_FILES['picture']['tmp_name'], $content_dir . $filename) ){
        exit("Couldn't write the file in $content_dir");
    }
}
else{
    // something when wrong
    exit("False");
}
// return
echo "True";
?>

Friday 9 February 2007

Upload Image to URL

Many people have been asking for help. Here is a working example of an image uploader:

Python script:

# Copyright (c) 2007 LEFEVRE Damien
# image uploader: python implementation
import httplib, urllib
import base64, os.path
 
 
def imageToURL( aPath ):
    # read the binary data of the picture
    data = open(aPath, 'rb').read()
    # encoded it to base64
    encodedData = base64.encodestring( data )
    headers = { "Content-type": "application/x-www-form-urlencoded",
                "Accept": "text/plain",
                }
 
    params = urllib.urlencode({ u'fileName': os.path.split(aPath)[1],
                                u'data':encodedData})
 
    conn = httplib.HTTPConnection( "yourURL.xxx" )
    conn.request( "POST", "/uploaderFolder/image_uploader.php", params, headers )
    response = conn.getresponse( )
    # returns "True" or "False" if failed
    print response.read( )
    # status for debugging
    print response.status
    conn.close( )
 
 
if __name__ == "__main__":
    imageToURL("yourImage.jpg")



Now the image uploader (image_uploader.php):

<?php
// Copyright (c) 2007 LEFEVRE Damien
// image_uploader.php implementation
 
// In this example a directory "images" needs to be present on the same directory where
// image_uploader.php is, with the necessary rights for the script to write data inside
 
if(isset($_POST['fileName'])){
    $filename = $_POST['fileName'];
}
else{
    // if not: just stop here
    print "False";
    die();
}
 
if(isset($_POST['data'])){
    $encodedData = $_POST['data'];
    $data = base64_decode($encodedData);
}
else{
    // if not: just stop here
    print "False";
    die();
}
 
// full path for the image
$filepathname = 'images/'.$filename;
 
// write the file to the server into the images directory
$handle = fopen($filepathname, 'wb');
fputs($handle, $data, strlen($data));
fclose($handle);
 
// return
echo "True";
?>

LEFEVRE Damien
http://www.lfdm.net
contact@lfdm.net