*/
class upload{
var $error_msg = '';
/**
* Constructor
*
* @param string $input_field_name form field name of uploaded file
*/
function upload($input_field_name){
$this->input_field_name = $input_field_name;
}
/**
* Set the maximum file size
*
* @param array $accepted_mime_types Accepted MIME-types
*/
function set_accepted_mime_types($accepted_mime_types){
$this->accepted_mime_types = $accepted_mime_types;
}
/**
* Set the maximum file size
*
* @param int $max_size Maximum file size in bytes
*/
function set_max_file_size($max_size){
$this->max_file_size = $max_size;
}
/**
* Sets the maximum pixel dimensions for image uploads
*
* @param int $width Maximum width of uploaded images (pixels)
* @param int $height Maximum height of uploaded images uploads
*/
function set_max_image_size($width, $height){
$this->max_image_width = $width;
$this->max_image_height = $height;
}
/**
* Draw a simple upload-form (all elements in one line).
*
* @param string $title
* @param string $action Value for the "action" attribute of the
";
}
/**
* Draw an upload-form
*
* @param string $title
* @param string $action Value for the "action" attribute of the ";
if (isset($this->accepted_mime_types)){
echo "This form only accepts the following MIME-types: " . implode(', ', $this->accepted_mime_types) . "
";
}
if (isset($this->max_file_size)){
$sz = $this->max_file_size / 1024;
echo "Maximum File size : ".$sz."kb
";
}
if (isset($this->max_image_width) && isset($this->max_image_height)){
echo "Maximum Image size: ".$this->max_image_width."x".$this->max_image_height."
";
}
}
/**
* Make some security-checks (e.g. file-size, MIME-type,...)
*/
function security_check(){
if (is_uploaded_file($_FILES[$this->input_field_name][tmp_name])){
$this->file = $_FILES[$this->input_field_name];
}else{
$this->error_msg = "Uploaded file does not exist!";
return false;
}
if(isset($this->max_file_size) && ($this->file["size"] > $this->max_file_size)){
$this->error_msg .= "Maximum file size exceeded . Uploaded files may not be larger than " . $this->max_file_size . " bytes . (= " . round($this->max_file_size / 1024, 2) . "KB)";
return false;
}
if(ereg("image", $this->file["type"])){
$image_size = getimagesize($this->file["tmp_name"]);
if(isset($this->max_image_width) && isset($this->max_image_height) &&
($image_size[0] > $this->max_image_width) || ($image_size[1] > $this->max_image_height)){
$this->error_msg .= "Maximum image size exceeded . Image may be no more than " . $this->max_image_width . " x " . $this->max_image_height . " pixels";
return false;
}
}
/**
* If the class should only allow some specific MIME-types,
* it will now check if the MIME-type is allowed.
*/
if(isset($this->accepted_mime_types) && !in_array($this->file["type"], $this->accepted_mime_types)){
$this->error_msg = "This MIME - type is not accepted!
\n";
$this->error_msg .= "Given: {$this->file["type"]}
\n";
$this->error_msg .= "Expected: " . implode(', ', $this->accepted_mime_types);
return false;
}
return true;
}
/**
* Moves the uploaded file
*
* @param string $destination_folder
* @param boolean $overwrite
*/
function move($destination_folder, $overwrite = false){
if ($this->security_check() == false){
return false;
}
$filename = $this->file['tmp_name'];
$destination = $destination_folder . $this->file['name'];
if (file_exists($destination) && $overwrite != true){
$this->error_msg = "This file already exists";
return false;
}elseif (move_uploaded_file ($filename, $destination)){
return true;
}else{
$this->error_msg = "Error moving the uploaded file!";
return false;
}
}
/**
* Read the content from the file and return it as a binary string.
*
* @return string The (binary) content of the file
*/
function read(){
if ($this->security_check() == false){
return false;
}
$filename = $this->file['tmp_name'];
$fd = fopen ($filename, "rb");
$contents = fread ($fd, filesize ($filename));
fclose ($fd);
return $contents;
}
}
?>