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"; ?>