Upload .jpg, .gif, .png, bmp, .swf, .psd to a folder on your local file system
By : Matt
<?php /*########################################################################### # Image Upload class- Upload .jpg, .gif, .png, bmp, .swf, .psd # to a folder on your local file system ############################################################################# # written by - George Mclachlan # e-mail - george@experimentalmonkey.co.uk # website - http://www.experimentalmonkey.co.uk # Any suggestions or bugs then please e-mail me ############################################################################# # Feel free to alter/modify the code in anyway, but please # leave this notice intact ############################################################################# # # Example Usage:- # Save this file as "UploadFile.class.php" # # Create a new PHP file and call it upload.php it should look like # ================================================================ # <?php # # require_once("UploadFile.class.php"); # //Delete the comments below or you will get parse errors # # $up = new UploadImage($nameofimagefield, <-name of your file field with $ # 102400, <-max file size in bytes # 100, <-max file height # 700, <-max file width # "banner", <-name of your file field # "c:/php/uploads", <-path to upload directory # $rename_file=true); <- default is true set to false # if you don't want the file renamed # # # if ($submit) { # if($up->ValidateUpload()) { # $up->CopyFile(); # } # } # $up->PrintForm(102400); <-max file size for html form same as above # ?> #############################################################################
*/
/*============================================== Random name function needed to provide a name for any files that are renamed =================================================*/
/*================================================ Validate the form- This method will validate the form and print out an error message if needed =================================================*/
function ValidateUpload() {
/*======================================== Get the size in kb of the file and prints an error is it exceed max_file_size ==========================================*/
if ($this->max_file_size < filesize($this->image)) {
print("<span style=\"color:red;\">ERROR: Your File: " .$_FILES[$this->field_name]["name"]." is ".filesize($this->image). " KB, the max file size allowed is " .$this->max_file_size."</span>");
return false;
}
/*=============================================== gets the width of the file and prints an error if file width is greater than max_file_width =================================================*/
$this->imagesize=getimagesize($this->image);
if ($this->max_file_width < $this->imagesize[0]) {
print("<span style=\"color:red;\">ERROR:<br />Your File: " .$_FILES[$this->field_name]["name"]." is ".$this->imagesize[0]. " pixels wide, the max file width allowed is " .$this->max_file_width."</span>");
return false;
}
/*=============================================== gets the height of the file and prints an error if file height is greater than max_file_width =================================================*/
if ($this->max_file_height < $this->imagesize[1]) {
print("<span style=\"color:red;\">ERROR:<br />Your File: " .$_FILES[$this->field_name]["name"]." is ".$this->imagesize[1]. " pixels high, the max file height allowed is " .$this->max_file_height."<span>");
return false;
}
/*=============================================== gets the filetype and checks it against allow types and returns an error if not in array =================================================*/
if (!in_array($this->imagesize[2], $this->allowed)) {
print("<span style=\"color:red;\">ERROR:<br />Your File: " .$_FILES[$this->field_name]["name"]." is not included in the list of allowed filetype</span>" );
return false;
}
/*================================== COOL:- no errors so lets get the file ====================================*/
return true; }
/*============================================ This mehod will print the form out on our page ==============================================*/
/*============================================= This method will copy our file to the folder of your choice and rename it if selected rename it ================================================*/
function CopyFile() { if($this->rename_file) {
/*==================================== this will find out our file extension which we need for renaming it. ========================================*/
global $name, $ext;
switch($_FILES[$this->field_name]["type"]) {
case 'image/gif'; $ext="gif"; break;
case 'image/jpeg'; $ext="jpg"; break;
case 'image/pjpeg'; $ext="jpg"; break;
case 'image/png'; $ext="png"; break;
case 'application/x-shockwave-flash'; $ext="swf"; break;
case 'image/psd'; $ext="psd"; break;
case 'image/bmp'; $ext="bmp"; break; }
/*======================================================== Let's rename our file and then copy it to the directory =========================================================*/
print("<b>There has been an error while uploading Filename:".$_FILES[$this->field_name]["name"] ." </b>");
} else {
print("Filename: ".$_FILES[$this->field_name]["name"] ." has been uploaded");
}
} else {
/*=================================================== if you don't want the file renamed then this will run let's make sure the file doesn't exist already and if it does let's return an error =====================================================*/
print("There has been an error uploading".$_FILES[$this->field_name]["name"]."please try adain");
} else {
print("Filename:".$_FILES[$this->field_name]["name"]." has been uploaded");
} } else {
print("ERROR: A file by this name already exists");
}
}
}
}
?>
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.