<CODE>
class UploadFile
{
function UploadFile()
{
$this->UpPath = "/home/virtual/site165/fst/var/www/uploads";
$this->MaxSize = 24576;
$this->MaxWidth = 170;
$this->MaxHeight = 220;
$this->RegTypes = array(
"image/gif" => ".gif",
"image/pjpeg" => ".jpg, .jpeg",
"image/jpeg" => ".jpg, .jpeg",
"image/tif" => ".tif, .tiff",
"image/png" => ".png",
);
$this->AllowedTypes = array("image/gif","image/pjpeg","image/jpeg","image/tif","image/png");
}
// The params can be reset and if they are NULL,
// the defaults will be kept for that param.
function NewParams($up_path,$max_size,$max_width,$max_height,$reg_types,$allowed_types)
{
if($up_path) {$this->UpPath = $up_path;}
if($max_size) {$this->MaxSize = $max_size;}
if($max_width) {$this->MaxWidth = $max_width;}
if($max_height) {$this->MaxHeight = $max_height;}
if($reg_types) {$this->RegTypes = $reg_types;}
if($allowed_types) {$this->AllowedTypes = $allowed_types;}
}
function ValidateFile($file_name)
{
global $_FILES;
$this->start_error = "\n<b>Error:</b>\n<ul>";
// Was there a file sent?
if (!$file_name) {
$this->error .= "\n<li>Please select a file to upload</li>";
// Check to see if the file is able to be uploaded given our perameters.
} else {
// If the file size is too big...
if ($_FILES["$file_name"]["error"] == 2) {
$this->error .= "\n<li>Your image size can be no larger than " . $this->MaxSize/1024 . "KB</li>";
} else {
// If the file type is NOT allowed...
if (!in_array($_FILES["$file_name"]["type"],$this->AllowedTypes)) {
$this->error .= "\n<li>The file that you uploaded: <b>".$_FILES["$file_name"]["name"]."</b> was of a type that is not allowed, you are only allowed to upload files of the types:\n<ul>";
while ($this->type = current($this->AllowedTypes)) {
$this->error .= "\n<li>" . $this->RegTypes[$type] . " (" . $this->type . ")</li>";
next($this->AllowedTypes);
}
$this->error .= "\n</ul>";
}
}
// If the file type is allowed AND the file size is not too big...
if (in_array($_FILES["$file_name"]["error"],$this->AllowedTypes) && $_FILES["$file_name"]["error"] != 2) {
$this->size = GetImageSize($_FILES["$file_name"]["tmp_name"]);
list($foo,$this->width,$bar,$this->height) = explode("\"",$this->size[3]);
if ($this->width > $this->MaxWidth) {
$this->error .= "\n<li>Your image should be no wider than " . $this->MaxWidth . " Pixels</li>";
}
if ($this->height > $this->MaxHeight) {
$this->error .= "\n<li>Your image should be no higher than " . $this->MaxHeight . " Pixels</li>";
}
}
if ($this->error) {
$this->error = $this->start_error . $this->error . "\n</ul>";
return false;
} else {
return true;
}
}
}
// Return the name of the file that was uploaded
function ShowFile()
{
return $this->the_file_name;
}
// Upload the file.
function Upload($file_name)
{
// Make sure we can upload the file
if ($this->ValidateFile($file_name)) {
global $LOGIN_INFO,$_FILES;
$SQLJunk = new MyDatabase;
$SQLJunk->Connect();
$this->x = false;
while ($this->x == false) {
// Make up a file name between 7 and 11 characters.
$this->the_file_name = PassGen(7,11);
// Get the extension of the file (ie. GIF or JPG) and add it to the file name.
$this->ext = explode("/",$_FILES["$file_name"]["type"]);
if($this->ext[1] == "pjpeg") {
$this->ext[1] = "jpg";
}
$this->the_new_file_name = $this->the_file_name.".".$this->ext[1];
$this->result = @mysql_query("SELECT image_name FROM images WHERE image_name='".$this->the_new_file_name."'");
if (!$this->row = mysql_fetch_array($this->result)) {
mysql_query("INSERT INTO images (login_id,image_name) VALUES ('".$LOGIN_INFO["login_id"]."','".$this->the_new_file_name."')") or die("Could not add pic name ($this->the_file_name) to DB<br><br>".mysql_error());
$this->x = true;
}
}
if (!@copy($_FILES["$file_name"]["tmp_name"], $this->UpPath . "/" . $this->the_new_file_name)) {
$this->error = "ERROR: An error has taken place while trying to upload the file. Please email E-link Design at info@elinkdesign.com and say that the file that could not be uploaded is: ".$this->the_file_name;
return false;
} else {
return true;
}
$SQLJunk->Disconnect();
} else {
return false;
}
}
// Return any Errors
function ReturnErrors()
{
return $this->error;
}
// Return MaxSize of file
function GetSize()
{
return $this->MaxSize;
}
}
</CODE>
My Buddy Sam wrote this for one of our projects.
/TCG
Thomas Chase Gullett
tgullett@elinkdesign.com