<?php
/**
file:fileupload.class.php File Upload class FileUpload
The instance object of this class is used to process the uploaded file, can upload a file, and can handle multiple file uploads at the same time.
*/
Class FileUpload {
Private $path = "./uploads"; Upload File Save path
Private $allowtype = array (' jpg ', ' gif ', ' PNG '); Set the type of limit upload file
Private $maxsize = 1000000; Limit file Upload size (bytes)
Private $israndname = true; Sets whether to randomly rename files, false not random
Private $originName; Source file name
Private $tmpFileName; Temporary file name
Private $fileType; File type (file suffix)
Private $fileSize; File size
Private $newFileName; New file name
Private $errorNum = 0; Error number
Private $errorMess = ""; Error Reporting messages
/**
* For setting member properties ($path, $allowtype, $maxsize, $israndname)
* Multiple property values can be set at once through a coherent operation
* @paramstring $key member property name (case insensitive)
* @parammixed $val The value set for the member property
* @returnobject return the object $this, can be used for coherent operation
*/
function set ($key, $val) {
$key = Strtolower ($key);
if (Array_key_exists ($key, Get_class_vars (Get_class ($this)))) {
$this->setoption ($key, $val);
}
return $this;
}
/**
* Call this method to upload a file
* @paramstring $fileFile form name for uploading files
* @returnbool If the upload succeeds return number true
*/
function upload ($fileField) {
$return = true;
/* Check the file path is a legal delay */
if (! $this->checkfilepath ()) {
$this->errormess = $this->geterror ();
return false;
}
/* Take out the file upload information to the variable */
$name = $_files[$fileField [' name '];
$tmp _name = $_files[$fileField] [' tmp_name '];
$size = $_files[$fileField [' size '];
$error = $_files[$fileField] [' ERROR '];
/* If multiple files are uploaded, $file["name"] will be an array */
if (Is_array ($name)) {
$errors =array ();
/* Multiple file uploads are recycled, this loop only checks the upload file's role, and does not actually upload */
for ($i = 0; $i < count ($name); $i + +) {
/* Set File information */
if ($this->setfiles ($name [$i], $tmp _name[$i], $size [$i], $error [$i]) {
if (! $this->checkfilesize () | |! $this->checkfiletype ()) {
$errors [] = $this->geterror ();
$return =false;
}
}else{
$errors [] = $this->geterror ();
$return =false;
}
/* If there is a problem, re-start the attribute */
if (! $return)
$this->setfiles ();
}
if ($return) {
/* Keep a variable array of all uploaded file names */
$fileNames = Array ();
/* If multiple uploaded files are legitimate, upload files to the server via Ecstasy loop */
for ($i = 0; $i < count ($name); $i + +) {
if ($this->setfiles ($name [$i], $tmp _name[$i], $size [$i], $error [$i]) {
$this->setnewfilename ();
if (! $this->copyfile ()) {
$errors [] = $this->geterror ();
$return = false;
}
$fileNames [] = $this->newfilename;
}
}
$this->newfilename = $fileNames;
}
$this->errormess = $errors;
return $return;
/* Upload a Single file processing method */
} else {
/* Set File information */
if ($this->setfiles ($name, $tmp _name, $size, $error)) {
/* Check the size and type before uploading */
if ($this->checkfilesize () && $this->checkfiletype ()) {
/* Set a new filename for the upload file */
$this->setnewfilename ();
/* Upload file returns 0 for success, less than 0 for error */
if ($this->copyfile ()) {
return true;
}else{
$return =false;
}
}else{
$return =false;
}
} else {
$return =false;
}
Error if $return is false, save error message in property errormess
if (! $return)
$this->errormess= $this->geterror ();
return $return;
}
}
/**
* Get uploaded file name
* @paramvoid no parameters
* @returnstring The name of the new file after uploading, if it is a multiple file upload return array
*/
Public Function getf Ilename () {
return $this->newfilename;
}
/**
* Upload failed, call this method return, upload error message
* @paramvoid no parameters
* @returnstring return the upload file error information Report, if it is multiple file upload return array
*/
Public Function geterrormsg () {
return $this->errormess;
}
/* Set upload error message */
Private Function GetError () {
$str = "Upload file <font color= ' red ' >{$this->originname} </font> error: ";
Switch ($this->errornum) {
Case 4: $str. = "No files are uploaded"; break;
Case 3: $str. = "file is only partially uploaded"; Break
Case 2: $str. = "The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form";
Case 1: $str. = "The uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini";
Case-1: $str. = "type not allowed";
Case-2: $str. = "File is too large to upload a file that cannot exceed {$this->maxsize} bytes";
Case-3: $str. = "Upload Failed";
Case-4: $str. = "Failed to set up the directory to upload files, please re-specify the upload directory";
Case-5: $str. = "The path of the uploaded file must be specified";
Default: $str. = "Unknown error";
}
return $str. ' <br> ';
}
/* Settings and $_files contents */
Private Function setfiles ($name = "", $tmp _name= "", $size =0, $error =0) {
$this SetOption (' ErrorNum ', $error);
if ($error)
return false;
$this->setoption (' Originname ', $name);
$this->setoption (' Tmpfilename ', $tmp _name);
$aryStr = Explode (".", $name);
$this->setoption (' FileType ', Strtolower ($aryStr [Count ($ARYSTR)-1]));
$this->setoption (' fileSize ', $size);
return true;
}
/* Set values for individual member properties */
Private Function SetOption ($key, $val) {
$this, $key = $val;
}
/* Set file name after upload */
Private Function Setnewfilename () {
if ($this->israndname) {
$this->setoption (' NewFileName ', $this->prorandname ());
} else{
$this->setoption (' NewFileName ', $this->originname);
}
}
/* Check if the uploaded file is a valid type */
Private Function Checkfiletype () {
if (In_array (Strtolower ($this FileType), $this->allowtype)) {
return true;
} else {
$this->setoption (' ErrorNum ',-1);
return false;
}
}
/* Check whether the uploaded file is the allowed size */
Private Function Checkfilesize () {
if ($this->filesize > $this- >maxsize) {
$this->setoption (' ErrorNum ',-2);
return false;
}else{
return true;
}
}
/* Check if there is a directory for uploading files */
Private Function Checkfilepath () {
if (Empty ($this->path)) {
$this->setoption (' ErrorNum ',-5);
return false;
}
if (!file_exists ($this->path) | |!is_writable ($this->path)) {
if ([email protected] ($this->path, 0755)) {
$this->setoption (' ErrorNum ',-4);
return false;
}
}
return true;
}
/* Set Random file name */
Private Function Prorandname () {
$fileName = Date (' Ymdhis '). " _ ". Rand (100,999);
Return $fileName. '. '. $this->filetype;
}
/* Copy the upload file to the specified location */
Private Function CopyFile () {
if (! $this->errornum) {
$path = RTrim ($this->path, '/'). ' /‘;
$path. = $this->newfilename;
if (@move_uploaded_file ($this->tmpfilename, $path)) {
return true;
}else{
$this->setoption (' ErrorNum ',-3);
return false;
}
} else {
return false;
}
}
}
File Upload Class