Using Form-based File Upload in Snoopy

Snoopy is capable of handling form-based file uploads according to RFC's 1867

and 2388.  This note describes how to use Snoopy in a number of different file

upload scenarios.  Snoopy will handle multiple file upload fields per request

and multiple files per field.  Snoopy can also suggest a different filename in

the upload request from the "real" local filename.  Snoopy can also put

Content-type into the file upload request on a per file basis.

 

 

EXAMPLE 1: SIMPLE FILE UPLOAD

 

In this example there is a single field with a single file to be uploaded and

nothing fancy.  Note that it is necessary to call the set_submit_multipart()

function for the file upload to work.  The filename suggested to the server

will be "myfile.txt" since the path will be removed.

 

include "Snoopy.class.inc";

$snoopy = new Snoopy;

 

$upload_url = "http://www.somedomain/upload.cgi";

$upload_vars = array();

$upload_files["FIELD1"] = "/home/me/myfile.txt";

 

$snoopy->set_submit_multipart();

 

if($snoopy->submit($upload_url, $upload_vars, $upload_files))

echo "<PRE>".$snoopy->results."</PRE>\n";

else

echo "error with file upload: ".$snoopy->error."\n";

 

 

 

EXAMPLE 2: MULTIPLE FILES IN A SINGLE FIELD

 

Snoopy will support sending multiple files in response to a single form field.

This example works when sending multiple files to an Apache/PHP server.  Other

servers may not support this in quite the same way.

 

 

include "Snoopy.class.inc";

$snoopy = new Snoopy;

 

$upload_url = "http://www.somedomain/upload.php";

$upload_vars = array();

$upload_files["FIELD1[]"] = array("/home/me/myfile1.txt", "/home/me/myfile2.txt");

 

$snoopy->set_submit_multipart();

 

if($snoopy->submit($upload_url, $upload_vars, $upload_files))

echo "<PRE>".$snoopy->results."</PRE>\n";

else

echo "error with file upload: ".$snoopy->error."\n";

 

 

 

EXAMPLE 3: SPECIFYING FILENAMES and CONTENT TYPES

 

Some hosts are fussy about the filename that is passed to them by the file

upload request.  We can overcome this by getting Snoopy to suggest a different

filename in the request than the "real" local filename.  We can also get

Snoopy to specify an optional content type for each file.  This is achieved by

having each file entry be an array of parameters instead of a simple filename.

 

include "Snoopy.class.inc";

$snoopy = new Snoopy;

 

$upload_url = "http://www.somedomain/upload.cgi";

$upload_vars = array();

$upload_files["FIELD1[]"] = array(

array("name" => "/home/me/myfile1.txt",

     "remotename" => "C:\UPLOAD.TXT",

     "type" => "text/plain"),

 

array("name" => "/home/me/myfile2.tiff",

     "remotename" => "C:\UPLOAD.TIF",

     "type" => "image/tiff")

);

 

$snoopy->set_submit_multipart();

 

if($snoopy->submit($upload_url, $upload_vars, $upload_files))

echo "<PRE>".$snoopy->results."</PRE>\n";

else

echo "error with file upload: ".$snoopy->error."\n";