< Back to Code Samples
<?php
/****************************************************************
* upload class
*
* PURPOSE:
* Provides the basis for the file attachment class. Handles the
* initial processing of uploaded files.
*
* It was written on a native IIS system ( :( ) which is why the
* directories listed are Windoze format.
*
* USAGE:
* $myFiles = array();
* foreach ($_FILES as $file) {
* $uploadedFile = new Upload($file, "/temp/");
* array_push($myFiles, $uploadedFile);
* }
*
******************************************************************/
class Upload {
protected $temp; // The temporary directory
protected $name; // The filename
function __construct($_file_array, $temp_dir = "D:\\inetpub\\temp\\") {
$this->temp = $temp_dir;
//
// $_file_array is a single element from the $_FILES supervariable.
//
if (!is_array($_file_array)) {
trigger_error("[ERR]: Invalid argument passed to " . __CLASS__, E_USER_NOTICE);
}
//
// Grab the filename from the file_array. If no such name exists, give it a temp name.
//
$this->name = (isset($_file_array['name']) && strlen($_file_array['name']) > 0)
? $_file_array['name']
: "php" . time() . ".tmp";
//
// Make sure the file can be moved to the specified temp directory!
//
if (!move_uploaded_file($_file_array['tmp_name'], $this->temp . $this->name)){
trigger_error("[ERR]: Unable to move uploaded file in " . __CLASS__, E_USER_NOTICE);
}
}
public function toString($mime_boundary) {
return $this->temp . $this->name;
}
}
?>