<?php
/**
*
* class uploader
*
* Copyright 1999, 2002, 2003 David Fox, Dave Tufts
*
* @version: 2.15
* @last_update: 2004-02-18
* @description: PHP file upload class
* @requires: PHP 4.1 or higher
*
* METHODS:
* uploader() - constructor
* max_filesize() - set a max filesize in bytes
* max_image_size() - set max pixel dimenstions for image uploads
* upload() - checks if file is acceptable, uploads file to server's temp directory
* save_file() - moves the uploaded file and renames it depending on the save_file($overwrite_mode)
*
* cleanup_text_file() - (PRIVATE) convert Mac and/or PC line breaks to UNIX
* get_error() - (PRIVATE) gets language-specific error message
* function change_type_of_file() change type of upload file
*
* Error code: available in English (en), Turkish (tr)
* [0] - "No file was uploaded"
* [1] - "Maximum file size exceeded"
* [2] - "Maximum image size exceeded"
* [3] - "Only specified file type may be uploaded"
* [4] - "File already exists" (save only)
* [5] - "Permission denied. Unable to copy file"
*
*/
class uploader {
var $file;
var $upload_file_name;
var $path;
var $acceptable_file_types;
var $error;
var $errors; // Depreciated (only for backward compatability)
var $accepted;
var $max_filesize;
var $max_image_width;
var $max_image_height;
var $type_of_file;
var $error_code;
/**
* object uploader ([string language]);
*
* Class constructor, sets error messaging language preference
*
* @param language (string) defaults to en (English).
*
* @examples: $f = new uploader(); // Turkish error messages
* $f = new uploader('en'); // English error messages
*
*/
function uploader () {
$this->error = 0;
$this->upload_file_name = '';
}
/**
* void max_filesize ( int size);
*
* Set the maximum file size in bytes ($size), allowable by the object.
* NOTE: PHP's configuration file also can control the maximum upload size, which is set to 2 or 4
* megs by default. To upload larger files, you'll have to change the php.ini file first.
*
* @param size (int) file size in bytes
*
*/
function max_filesize($size){
$this->max_filesize = (int) $size;
}
/**
* void max_image_size ( int width, int height );
*
* Sets the maximum pixel dimensions. Will only be checked if the
* uploaded file is an image
*
* @param width (int) maximum pixel width of image uploads
* @param height (int) maximum pixel height of image uploads
*
*/
function max_image_size($width, $height){
$this->max_image_width = (int) $width;
$this->max_image_height = (int) $height;
}
/**
* bool upload (string filename[, string accept_type[, string extension]]);
*
* Checks if the file is acceptable and uploads it to PHP's default upload diretory
*/
function upload() {
$this->acceptable_file_types = trim($this->accept_type); // used by error messages
$filename = $this->upload_file_name;
if (!isset($_FILES) || !is_array($_FILES[$filename]) || !$_FILES[$filename]['name']) {
$this->error = $this->get_error(6);
$this->accepted = FALSE;
return FALSE;
}
// Copy PHP's global $_FILES array to a local array
$this->file = $_FILES[$filename];
$this->file['file'] = $filename;
// Initialize empty array elements
if (!isset($this->file['extention'])) $this->file['extention'] = "";
if (!isset($this->file['type'])) $this->file['type'] = "";
if (!isset($this->file['size'])) $this->file['size'] = "";
if (!isset($this->file['width'])) $this->file['width'] = "";
if (!isset($this->file['height'])) $this->file['height'] = "";
if (!isset($this->file['tmp_name'])) $this->file['tmp_name'] = "";
if (!isset($this->file['raw_name'])) $this->file['raw_name'] = "";
// test max size
if($this->max_filesize && ($this->file["size"] > $this->max_filesize)) {
$this->error = $this->get_error(1);
$this->accepted = FALSE;
return FALSE;
}
if(stristr($this->file["type"], "image")) {
/* IMAGES */
$image = getimagesize($this->file["tmp_name"]);
$this->file["width"] = $image[0];
$this->file["height"] = $image[1];
$this->file["type"] = $image[2];
$this->file["properties"] = $image[3];
// test max image size
if(($this->max_image_width || $this->max_image_height) && (($this->file["width"] > $this->max_image_width) || ($this->file["height"] > $this->max_image_height))) {
$this->error = $this->get_error(2);
$this->accepted = FALSE;
return FALSE;
}
// Image Type is returned from getimagesize() function
switch($image[2]) {
case 1:
$this->file["extention"] = ".gif"; break;
case 2:
$this->file["extention"] = ".jpg"; break;
case 3:
$this->file["extention"] = ".png"; break;
case 4:
$this->file["extention"] = ".swf"; break;
case 5:
$this->file["extention"] = ".psd"; break;
case 6:
$this->file["extention"] = ".bmp"; break;
case 7:
$this->file["extention"] = ".tif"; break;
case 8:
$this->file["extention"] = ".tif"; break;
default:
$this->file["extention"] = $this->extention; break;
}
} elseif(!ereg("(\.)([a-z0-9]{3,5})$", $this->file["name"]) && !$this->extention) {
// Try and autmatically figure out the file type
// For more on mime-types: http://httpd.apache.org/docs/mod/mod_mime_magic.html
switch($this->file["type"]) {
case "text/plain":
$this->file["extention"] = ".txt"; break;
case "text/richtext":
$this->file["extention"] = ".txt"; break;
default:
break;
}
} else {
$this->file["extention"] = $this->extention;
}
// check to see if the file is of type specified
if($this->acceptable_file_types) {
if(trim($this->file["type"]) && (stristr($this->acceptable_file_types, $this->file["type"]) || stristr($this->file["type"], $this->acceptable_file_types)) ) {
$this->accepted = TRUE;
} else {
$this->accepted = FALSE;
$this->error = $this->get_error(3);
}
} else {
$this->accepted = TRUE;
}
return (bool) $this->accepted;
}
/**
* bool save_file ( string path[, int overwrite_mode] );
*
* Cleans up the filename, copies the file from PHP's temp location to $path,
* and checks the overwrite_mode
*
* @param path (string) File path to your upload directory
* @param overwrite_mode (int) 1 = overwrite existing file
* 2 = rename if filename already exists (file.txt becomes file_copy0.txt)
* 3 = do nothing if a file exists
*
*/
function save_file(){
$path = $this->path;
$overwrite_mode = $this->mode;
if ($this->error) {
return false;
}
if (strlen($path)>0) {
if ($path[strlen($path)-1] != "/") {
$path = $path . "/";
}
}
$this->path = $path;
$copy = "";
$n = 1;
$success = false;
if($this->accepted) {
//remove any turkish characters
$this->file["name"] = tr2en_lowercase($this->file["name"]);
// Clean up file name (only lowercase letters, numbers and underscores)
$this->file["name"] = ereg_replace("[^a-z0-9._]", "", str_replace(" ", "_", str_replace("%20", "_", strtolower($this->file["name"]))));
// Clean up text file breaks
if(stristr($this->file["type"], "text")) {
$this->cleanup_text_file($this->file["tmp_name"]);
}
// get the raw name of the file (without its extenstion)
if(ereg("(\.)([a-z0-9]{2,5})$", $this->file["name"])) {
$pos = strrpos($this->file["name"], ".");
if(!$this->file["extention"]) {
$this->file["extention"] = substr($this->file["name"], $pos, strlen($this->file["name"]));
}
$this->file['raw_name'] = substr($this->file["name"], 0, $pos);
} else {
$this->file['raw_name'] = $this->file["name"];
if ($this->file["extention"]) {
$this->file["name"] = $this->file["name"] . $this->file["extention"];
}
}
switch((int) $overwrite_mode) {
case 1: // overwrite mode
if (@copy($this->file["tmp_name"], $this->path . $this->file["name"])) {
$success = true;
} else {
$success = false;
$this->error = $this->get_error(5);
}
break;
case 2: // create new with incremental extention
while(file_exists($this->path . $this->file['raw_name'] . $copy . $this->file["extention"])) {
$copy = "_copy" . $n;
$n++;
}
$this->file["name"] = $this->file['raw_name'] . $copy . $this->file["extention"];
if (@copy($this->file["tmp_name"], $this->path . $this->file["name"])) {
$success = true;
} else {
$success = false;
$this->error = $this->get_error(5);
}
break;
default: // do nothing if exists, highest protection
if(file_exists($this->path . $this->file["name"])){
$this->error = $this->get_error(4);
$success = false;
} else {
if (@copy($this->file["tmp_name"], $this->path . $this->file["name"])) {
$success = true;
} else {
$success = false;
$this->error = $this->get_error(5);
}
}
break;
}
if(!$success) { unset($this->file['tmp_name']); }
return (bool) $success;
} else {
$this->error = $this->get_error(3);
return FALSE;
}
}
/**
* string get_error(int error_code);
*
* Gets the correct error message for language set by constructor
*
* @param error_code (int) error code
*
*/
function get_error($error_code='') {
$error_message = array();
$error_code = (int) $error_code;
$this->error_code = $error_code;
return $error_code;
}
/**
* void cleanup_text_file (string file);
*
* Convert Mac and/or PC line breaks to UNIX by opening
* and rewriting the file on the server
*
* @param file (string) Path and name of text file
*
*/
function cleanup_text_file($file){
// chr(13) = CR (carridge return) = Macintosh
// chr(10) = LF (line feed) = Unix
// Win line break = CRLF
$new_file = '';
$old_file = '';
$fcontents = file($file);
while (list ($line_num, $line) = each($fcontents)) {
$old_file .= $line;
$new_file .= str_replace(chr(13), chr(10), $line);
}
if ($old_file != $new_file) {
// Open the uploaded file, and re-write it with the new changes
$fp = fopen($file, "w");
fwrite($fp, $new_file);
fclose($fp);
}
}
function change_type_of_file ( $type_of_file = '' ) {
$this->upload_file_name = "userfile";
$this->acceptable_file_types = "image/gif|image/jpeg|image/pjpeg";
$this->default_extension = "";
$this->mode = 1;
switch ($type_of_file){
default:
$this->max_filesize(C_MAX_FILESIZE*1024); // byte
$this->max_image_size(C_MAX_PICTURE_WH, C_MAX_PICTURE_WH); // piksel
break;
}
}
function echo_variables () {
echo "path:".$this->path."<br>";
echo "upload_file_name:".$this->upload_file_name."<br>";
print_r($this->acceptable_file_types);
echo "<br>";
echo "default_extension:".$this->default_extension."<br>";
echo "mode:".$this->mode."<br>";
echo "path:".$this->path."<br>";
echo "max_filesize:".$this->max_filesize."<br>";
echo "max_image_width:".$this->max_image_width."<br>";
echo "max_image_height:".$this->max_image_height."<br>";
}
}
/*
<readme>
fileupload-class.php can be used to upload files of any type
to a web server using a web browser. The uploaded file's name will
get cleaned up - special characters will be deleted, and spaces
get replaced with underscores, and moved to a specified
directory (on your server). fileupload-class.php also does its best to
determine the file's type (text, GIF, JPEG, etc). If the user
has named the file with the correct extension (.txt, .gif, etc),
then the class will use that, but if the user tries to upload
an extensionless file, PHP does can identify text, gif, jpeg,
and png files for you. As a last resort, if there is no
specified extension, and PHP can not determine the type, you
can set a default extension to be added.
SETUP:
Make sure that the directory that you plan on uploading
files to has enough permissions for your web server to
write/upload to it. (usually, this means making it world writable)
- cd /your/web/dir
- chmod 777 <fileupload_dir>
The HTML FORM used to upload the file should look like this:
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="userfile">
<input type="submit" value="Submit">
</form>
USAGE:
// Create a new instance of the class
$my_uploader = new uploader;
// OPTIONAL: set the max filesize of uploadable files in bytes
$my_uploader->max_filesize(90000);
// OPTIONAL: if you're uploading images, you can set the max pixel dimensions
$my_uploader->max_image_size(150, 300); // max_image_size($width, $height)
// UPLOAD the file
$my_uploader->upload("userfile", "", ".jpg");
// MOVE THE FILE to its final destination
// $mode = 1 :: overwrite existing file
// $mode = 2 :: rename new file if a file
// with the same name already
// exists: file.txt becomes file_copy0.txt
// $mode = 3 :: do nothing if a file with the
// same name already exists
$my_uploader->save_file("/your/web/dir/fileupload_dir", int $mode);
// Check if everything worked
if ($my_uploader->error) {
echo $my_uploader->error . "<br>";
} else {
// Successful upload!
$file_name = $my_uploader->file['name'];
print($file_name . " was successfully uploaded!");
}
$my_uploader = new uploader;
$my_uploader->change_type_of_file("C_ROOMS");
$my_uploader->max_filesize(150000);
$my_uploader->max_image_size(400, 400);
$my_uploader->path = strtolower("./siteimages/artist/small/");
//$my_uploader->echo_variables();
// UPLOAD the file
if ($my_uploader->upload()) {
$my_uploader->save_file();
}
if ($my_uploader->error) {
echo $my_uploader->error . "<br>";
}
else {
// Successful upload!
$file_name = $my_uploader->file['name'];
print($file_name . " was successfully uploaded!");
}
</readme>
*/
?>