Home > Backend Development > PHP Tutorial > A very useful file upload class in PHP_PHP tutorial

A very useful file upload class in PHP_PHP tutorial

WBOY
Release: 2016-07-15 13:21:31
Original
869 people have browsed it


class FileUpload{

private $filepath; //Set the path to upload files
private $allowtype=array('jpg','jpeg','gif','bmp'); //Default file type
private $maxsize=2000000; //Default file size
private $israndname=true; //Whether to use a random file name
private $originName; //Source file name
private $tmpFileName; //Temporary file name
private $fileType; //File type
private $fileSize; //The size of the file
private $newFileName; //The name of the new file
private $errorNum; //Error number
private $errorMess=""; //Used to prompt error reports
//Function used for file initialization
function __construct($options=array()){
foreach($options as $key=>$val){
$key=strtolower($key);//Set attribute names to lowercase
//get_class_vars(get_class($this))
//Get all attributes of the current class
If(!in_array($key,get_class_vars(get_class($this)))){
Continue;
}
else
{
$this->setOption($key,$val);
}
}
}

//Define different error levels
private function getError(){
$str="Error uploading file {$this->originName}:";


switch($this->errorNum){
case 4:
$str.="File not uploaded";
Break;
case 3:
$str.="Only part of the file is uploaded";
Break;
Case 2:
$str.="The uploaded file exceeds the value of the MAX_FILE_SIZE option specified in the HTML form";
Break;
Case 1:
$str.="The uploaded file exceeds the value of the upload_max_filesize option in php.ini";
Break;
case -1:
$str.="Unallowed type";
Break;
case -2:
$str.="The uploaded file is too large and cannot exceed {$this->maxSize} bytes";
Break;
case -3:
$str.="Upload failed";
Break;
case -4:
$str.="Failed to create the upload directory, please re-specify the upload directory";
Break;
case -5:
$str.="The path to the uploaded file must be specified";
Break;
Default:
$str.="Unknown error";

}

return $str."
";
}

//Function to assign values ​​to member attributes
private function setOption($key,$val){
$this->$key=$val;
}
// Used to check the path of uploaded files
private function checkFilePath(){
//If the file path is empty
if(empty($this->filepath)){
$this->setOption('errorNum',-5);
Return false;
}
//Determine whether the path exists and is writable
If(!file_exists($this->filepath)||!is_writable($this->filepath)){
//@ is the error suppressor @ is to ignore the error prompt and make it error
//Error messages will not be displayed in the program
If(!@mkdir($this->filepath,0755)){
$this->setOption('errorNum',-4);
Return false;
}
}
Return true;
}

//Function to check file size
private function checkFileSize(){
if($this->fileSize>$this->maxsize){
$this->setOption("errorNum",-2);
Return false;
}
else
{
Return true;
}

}

//Check the type of uploaded file
private function checkFileType(){
If(in_array(strtolower($this->fileType),$this->allowtype))
{
Return true;
}
else
{
$this->setOption("errorNum",-1);
Return false;
}
}
//Upload a file
function uploadFile($fileField){
$return=true;
$name=$_FILES[$fileField]["name"]; //Name submitted by post
$tmp_name=$_FILES[$fileField]["tmp_name"]; //Temporary file name when uploading
$size=$_FILES[$fileField]["size"];
$error=$_FILES[$fileField]["error"];
If($this->setFiles($name,$tmp_name,$size,$error)){
If($this->checkFileSize() && $this->checkFileType()){
$this->setNewFileName();
If($this->copyFile())
{
       $return=true;
}
      else
{
       $return=false;
}
}
else
{
$return=false;
}
}
else
{
$return=false;
}
//Check the file path for errors
if(!$this->checkFilePath()){
$this->errorMess=$this->getError();
Return false;
}

if(!$return)
{
$this->errorMess=$this->getError();
}
Return $return;
}
//Set content related to $_FILES
private function setFiles($name="",$tmp_name="",$size=0,$error=0){
$this->setOption("errorNum",$error);
if($this->errorNum){
Return false;
}
$arrstr=explode(".",$name);
$this->setOption("fileType",strtolower($arrstr[count($arrstr)-1]));
$this->setOption("originName",$name);
$this->setOption("tmpFileName",$tmp_name);
$this->setOption("fileSize",$size);
Return true;
}

//Used to get the name of the uploaded file
function getNewFileName(){
Return $this->newFileName.'Upload successful! ';
}


//Function to display error message when uploading file fails
function getErrorMsg(){
Return $this->errorMess;
}
private function proRandName(){
$filename=date("YmdHis").rand(100,999);
Return $filename.".".$this->fileType;
}

//Set the uploaded file name
private function setNewFileName(){
if($this->israndname){
$this->setOption('newFileName',$this->proRandName());
}
else
{
$this->setOption('newFileName',$this->originName);
}
}

//Copy file
private function copyFile(){
if(!$this->errorNum){
$filepath=rtrim($this->filepath,'/')."/";
$filepath.=$this->newFileName;
//Copy the file to the specified path
If(@move_uploaded_file($this->tmpFileName,$filepath)){
Return true;
}
else
{
$this->setOption('errorNum',-3);
Return false;
}
}
}


}

?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477198.htmlTechArticle?php class FileUpload{ private $filepath; //Set the path to upload files private $allowtype=array(jpg, jpeg,gif,bmp); //Default file type private $maxsize=2000000; //Default file...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template