PHP File Upload category,
<? Php
/**
* File Upload class
* @ Author lijiamin
* @ Time 2017-02-17
* @ Email 1195989301@qq.com
*/
Class Upload {
Private $ allowExt = array ('gif', 'jpg ', 'jpeg', 'bmp ', 'png', 'swf '); // restrict the suffix of a file to be uploaded.
Private $ maxSize = 1; // maximum file upload size: 1 MB
/**
* Getting File Information
* @ Param str $ flag ID of the uploaded file
* @ Return arr array of information about the file to be uploaded
*/
Public function getInfo ($ flag ){
Return $ _ FILES [$ flag];
}
/**
* Get the file suffix
* @ Param str $ filename file name
* @ Return str File Extension
*/
Public function getExt ($ filename ){
Return pathinfo ($ filename, PATHINFO_EXTENSION );
}
/**
* Checks whether the uploaded file is valid.
* @ Param str $ filename file name
* @ Return whether the bool file extension is valid
*/
Private function checkExt ($ filename ){
$ Ext = $ this-> getExt ($ filename );
Return in_array ($ ext, $ this-> allowExt );
}
/**
* Check whether the file size exceeds the limit.
* @ Param int size file size
* @ Return whether the bool file size exceeds the limit
*/
Public function checkSize ($ size ){
Return $ size <$ this-> maxSize * 1024*1024;
}
/**
* Random file names
* @ Param int $ len: length of random file names
* @ Return str random string
*/
Public function randName ($ len = 6 ){
Return substr (str_shuffle ('abcdefghjkmnpqrstuvwxyzabcdefghjklmnpqrstuvwxyz234565789 '), 0, $ len );
}
/**
* Create a file upload path
* @ Return str File Upload path
*/
Public function createDir (){
// Upload File Path
$ Dir = './upload/'. date ('y/m/d', time ());
// Check whether the folder exists. If it does not exist, create a folder.
If (is_dir ($ dir) | mkdir ($ dir, 0777, true )){
Return $ dir;
}
}
/**
* File Upload
* @ Param str $ flag File Upload ID
* @ Return array returns the Upload File Name and storage 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 types ";
Return;
}
If (! $ This-> checkSize ($ info ['SIZE']) {
Echo "the file size exceeds the 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 ";
} Else {
Return array ('filename' => $ filename, 'dir' => $ dir );
}
}
}
?>