English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP File Upload Classification Example Code

No more chatter, let's get straight to the code. Here is the specific code as follows:

<?php
/**
* File upload class
* @author lijiamin
* @time 2017-02-17
* @email [email protected]
*/
class Upload{
private $allowExt = array('gif','jpg','jpeg','bmp','png','swf');//limit the file upload extensions
private $maxSize = 1;//limit the maximum file upload1M
/**
* get file information
* @param str $flag identifier of the uploaded file
* @return arr array of file upload information
*/
public function getInfo($flag){
return $_FILES[$flag];
}
/**
* get the file suffix
* @param str $filename filename
* @return str file extension
*/
public function getExt($filename){
return pathinfo($filename,PATHINFO_EXTENSION);
}
/**
* check if the uploaded file is valid
* @param str $filename filename
* @return bool whether the file extension is valid
*/
private function checkExt($filename){
$ext = $this->getExt($filename);
return in_array($ext,$this->allowExt
}
/**
* check if the file size exceeds the limit
* @param int size file size
* @return bool whether the file size exceeds the limit
*/
public function checkSize($size){
return $size < $this->maxSize * 1024 * 1024;
}
/**
* random filename
* @param int $len length of random filename
* @return str random string
*/
public function randName($len=6){
return substr(str_shuffle('abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ234565789'),0,$len);
}
/**
* create the path where the file will be uploaded
* @return str file upload path
*/ 
public function createDir(){
//upload file path
$dir = '.'/upload/'.date('Y/m/d',time());
//determine if the folder exists, and create a new one if it does not
if(is_dir($dir) || mkdir($dir,0777,true)){
return $dir;
}
}
/**
* File Upload
* @param str $flag File upload identifier
* @return array Returns the uploaded file name and save path
*/
public function uploadFile($flag){
if($_FILES[$flag]['name'] === '' || $_FILES[$flag]['error'] !== 0){
echo "No file uploaded";
return;
}
$info = $this->getInfo($flag);
if(!$this->checkExt($info['name'])){
echo "Unsupported file type";
return;
}
if(!$this->checkSize($info['size'])){
echo "File size exceeds limit";
return;
}
$filename = $this->randName().'.'.$this->getExt($info['name']);
$dir = $this->createDir();
if(!move_uploaded_file($info['tmp_name'], $dir.'/'.$filename')){
echo "File upload failed";
}
return array('filename'=>$filename,'dir'=>$dir);
}
}
}
?>

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like