Understood and agreed. I was trying to avoid taking up a lot of space in a message with the entire script - but it’s a great (old) script and maybe others will find it useful:
<?php
###################################################################################################
File Upload Class - v1.0
Written By startx - Jacob Wyke
###################################################################################################
This class handles all your file uploading needs. It can handle most file types - you can add
more if needed (just add them to the array in the file_types() function. To use this class
for yourself do the following:
USAGE:
##-------------------------------------------------------------------------------------------------
include(‘this_file_name.php’);
$upload_class = new File_Upload(‘upload directory’,
‘max_file_size’,
‘file__types_array’,
‘upload_field_name’,
‘upload_fix_prefix’
);
if(!$_POST[submit]){
#shows the default upload form included with the class - you can make your own
echo $upload_class->default_form(‘file_upload.php’);
}else{
$image[width] = ‘150’; #max image width
$image[height] = ‘350’; #max image height
$image[thumbs] = ‘0’; #make thumbnail of image
$image[thumb_width] = ‘50’; #thumbnail width
$image[thumb_height] = ‘50’; #thumbnail height
$image[resize] = ‘1’; #should images be automatically resized
$image[skip_resize] = ‘0’; #skip image resizing - shows no “image too big” error
#processes the submitted form and returns any error or success message
echo $upload_class->process($image);
#array containing the location of all uploaded files so you can add them to a database/file etc
$uploaded_files = $upload_class->output;
}
##-------------------------------------------------------------------------------------------------
The example above shows you how to control the class if you wish to only upload images,
if you want to just upload files to the server then do the same except exclude all the
$image array vars and DO NOT pass it to the “echo $upload_class->process($image);” line
e.g.
echo $upload_class->process();
Further explainations for calling the class
‘upload directory’ - The directory where you want to store the uploaded files
Make sure you make it writable by the server.
‘max_file_size’, - This is the maximum allowed file size to be uploaded in bytes.
Default is set to 51200 which is 500kb
‘file_types_array’ - This is an array containing your own file types. By default
the script allows all the file types shown in the file_types() function
but if you pass your own array of them - then only those types are
allowed. This allows you to limit the types of files. You could of
course just delete any unwanted types from the array in file_types()
‘upload_field_name’ - This is the name of the file field in the form - only use if you
make your own form and dont use the one included in this class.
‘upload_fix_prefix’ - Allows you to add a prefix to all files you upload so that you
can better organise them.
All of these are optional and so you can just call the class with ‘new File_Upload();’
###################################################################################################
LICENSE
Please feel free to use/edit this class as you wish but for non-commercial uses ONLY. If
you wish to use it for a commercial product that will be sold then please contact me
further for usage details. You must also keep this license and my contact details attached
somewhere to this class file.
Other than that please feel free to use and play with it and give me feedback and info
on what your using it for
Enjoy!
###################################################################################################
class File_Upload{
var $allow_types;
var $field_name;
var $dir;
var $file_prefix;
var $file_max_size;
var $location;
var $thumb_location;
var $output;
##--------------------------------------------
##CLASS CONSTRUCTOR
##--------------------------------------------
function File_Upload($dir = ‘uploads’, $size = ‘512000’, $types = ‘0’, $name = ‘upload’){
#creates array of allowed file types if you dont enter your own
if($types == ‘0’){
$this->file_types();
}else{
$this->file_types = $types;
}
$this->field_name = $name; #name of the upload field in form
$this->dir = $dir; #name of upload directory
$this->file_prefix = $_POST['uid']; #file prefix
$this->file_max_size = $size; #max file size
$this->output = array (); #makes array to store loction of uploaded files so you know what was uploaded
}
##--------------------------------------------
##FILE TYPES FUNCTION
##Fills the array with the types of files you want to allow
##You can add your own list of file types to the array below
##--------------------------------------------
function file_types(){
$this->allow_types = array (
‘image/gif’,
‘image/x-png’,
‘image/jpeg’,
‘image/pjpeg’,
);
}
##--------------------------------------------
##PROCESS FUNCTION
##Processes the file upload
##--------------------------------------------
function process($image = ‘0’){
#checks that file was uploaded
if(!file_exists($_FILES[$this->field_name][tmp_name])){
return $this->error(1);
}else{
#checks that the file isnt too large
if($_FILES[$this->field_name][size]>$this->file_max_size){
return $this->error(2);
}else{
#checks to see if file type is allowed
if(!in_array($_FILES[$this->field_name][type],$this->allow_types)){
return $this->error(3);
}else{
if($_FILES[$this->field_name][type]=='image/gif')
$ext='.gif';
if($_FILES[$this->field_name][type]=='image/x-png')
$ext='.png';
if($_FILES[$this->field_name][type]=='image/jpeg')
$ext='.jpg';
if($_FILES[$this->field_name][type]=='image/pjpeg')
$ext='.jpg';
#Repeat loop till you find an available file name
$loop = '0';
do{
$loop++;
#create new file location
if(file_exists($this->dir."/".$this->file_prefix.".jpg"))
unlink($this->dir."/".$this->file_prefix.".jpg");
if(file_exists($this->dir."/".$this->file_prefix.".gif"))
unlink($this->dir."/".$this->file_prefix.".gif");
if(file_exists($this->dir."/".$this->file_prefix.".png"))
unlink($this->dir."/".$this->file_prefix.".png");
$this->location = $this->dir."/".$this->file_prefix.$ext;
if(file_exists($this->dir."/".$this->file_prefix."_t".".jpg"))
unlink($this->dir."/".$this->file_prefix."_t".".jpg");
if(file_exists($this->dir."/".$this->file_prefix."_t".".png"))
unlink($this->dir."/".$this->file_prefix."_t".".png");
if(file_exists($this->dir."/".$this->file_prefix."_t".".gif"))
unlink($this->dir."/".$this->file_prefix."_t".".gif");
$this->thumb_location = $this->dir."/".$this->file_prefix."_t".$ext;
}while(file_exists($this->location));
#processes file for image stuff
if($image){
$outcome = $this->process_image($image);
if(!$image[skip_resize]){ #dont need to rename the orignal as we have skipped the resizing
$this->location = $this->dir."/orig_".$loop."_".$this->file_prefix.$_FILES[$this->field_name][name];
}
}
if($outcome){
#returns any errors from the image function
return $outcome;
}else{
return "1";
}
}
}
}
}
##--------------------------------------------
##PROCESS IMAGE FUNCTION
##Processes images
##--------------------------------------------
function process_image($image){
#gets the image dimensions
$size = getimagesize($_FILES[$this->field_name][tmp_name]);
#makes sure the image isnt larger than your max width/height
if((($size[0]>$image[width]) OR ($size[1]>$image[height])) AND !$image[resize] AND !$image[skip_resize]){
return $this->error(5);
}else if(($size[0]>$image[width]) OR ($size[1]>$image[height]) AND $image[resize]){
#resize the image if wanted
if(!$image[skip_resize]){
$height=(int)(($image[width]*$size[1])/$size[0]);
$this->make_image($this->location, $image, $size, $image[width], $height);
}
}else{
#send image as is if no resize is needed
$this->make_image($this->location, $image, $size, $size[0], $size[1]);
}
#make thumbnail of image if needed
if($image[thumbs]){
$this->make_image($this->thumb_location, $image, $size, $image[thumb_width], $image[thumb_height]);
}
}
##--------------------------------------------
##MAKE IMAGE FUNCTION
##Makes image
##--------------------------------------------
function make_image($location, $image, $size, $width, $height){
if($_FILES[$this->field_name][type]==‘image/gif’){
if(function_exists(‘imagecreatefromgif’)){
$im = @imagecreatefromgif($_FILES[$this->field_name][tmp_name]);
}else{
echo $this->error(6);
exit;
}
}
if($_FILES[$this->field_name][type]==‘image/jpeg’ OR $_FILES[$this->field_name][type]==‘image/pjpeg’){
if(function_exists(‘imagecreatefromjpeg’)){
$im = @imagecreatefromjpeg($_FILES[$this->field_name][tmp_name]);
}else{
echo $this->error(6);
exit;
}
}
if($_FILES[$this->field_name][type]==‘image/x-png’){
if(function_exists(‘imagecreatefrompng’)){
$im = @imagecreatefrompng($_FILES[$this->field_name][tmp_name]);
}else{
echo $this->error(6);
exit;
}
}
if(!$im){
echo $this->error(7);
exit;
}else{
$dst_img = imagecreatetruecolor($width,$height);
imagecopyresized($dst_img, $im, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagejpeg($dst_img, $location, 75);
array_push($this->output, $location);
}
}
##--------------------------------------------
##ERROR FUNCTION
##Returns error messages
##--------------------------------------------
function error($num){
switch($num){
case ‘1’:
$err = “There was an error uploading the file to the server.”;
break;
case ‘2’:
$err = "The uploaded file was too large - max size is ".round($this->file_max_size/5120).“kb”;
break;
case ‘3’:
$err = “This server does not accept the file type you attempted to upload.”;
break;
case ‘4’:
$err = “The uploaded file could not be moved from its temporary location.”;
break;
case ‘5’:
$err = “The image dimensions were too big.”;
break;
case ‘6’:
$err = “This type of image cannot be manipulated.”;
break;
case ‘7’:
$err = “An error occured when resizing the image.”;
break;
}
return $err;
}
##--------------------------------------------
##DEFAULT FORM FUNCTION
##Shows a very basic form for the upload
##--------------------------------------------
function default_form($action, $class = ‘’, $button_class = ‘’){
$text = "
<form method=‘POST’ action=‘$action’ name=‘file_upload’ enctype=‘multipart/form-data’>
File: <input class=‘$class’ type=‘file’ name=‘upload’ size=‘25’>
<br />
<input type=‘hidden’ value=‘414’ name=‘uid’>
<input class=‘$button_class’ type=‘submit’ name=‘submit’ value=‘Upload’>
</form>
";
return $text;
}
}
if($_POST[‘uid’])
{ $upload_class = new File_Upload(‘C:\\Inetpub\\wwwroot\\[foo.com]\\user_images\\’,1548576,‘0’,‘upload’);
$image[width] = "150"; #max image width
$image[height] = "350"; #max image height
$image[thumbs] = "1"; #make thumbnail of image
$image[thumb_width] = "50"; #thumbnail width
$image[thumb_height] = "50"; #thumbnail height
$image[resize] = "1"; #should images be automatically resized
$image[skip_resize] = "0"; #skip image resizing - shows no "image too big" error
$result = $upload_class->process($image);
if($result=="1")
header("Location: ./sitter_main.asp");
else
header("Location: ./sysmsg.asp?msg=".$result);
}
?>