Ec (2); Complete PHP File Upload class classUploadFile {& nbsp; ** & nbsp; * supported File Types & nbsp; * @ vararray $ allowFileTypes & nbsp; * @ accessprivate & nbsp; * & nbsp; private $ allowFileTypesarray (jpeg, jpg, gif, script ec (2); script
Complete php File Upload class
Class UploadFile {
/**
* Supported file types
* @ Var array $ allowFileTypes
* @ Access private
*/
Private $ allowFileTypes = array ('jpeg ', 'jpg', 'gif', 'bmp ', 'png ');
/**
* Size of the file to be uploaded, in bytes
* @ Var int $ maxFileSize
* @ Access public
*/
Public $ Max filesize = 8388608;
/**
* Constructor
*/
Public function _ construct (){
}
/**
* Set the allowed file types
* @ Param mixed $ the list of fileTypes file types can be arrays and strings separated by commas (,).
* @ Return void
* @ Access public
*/
Public function setAllowFileType ($ fileTypes ){
If (! Is_array ($ fileTypes )){
$ This-> allowFileTypes = explode (',', $ fileTypes );
} Else {
$ This-> allowFileTypes = $ fileTypes;
}
Return;
}
/**
* Upload a file
* @ Param string $ the file to be uploaded by fileField, for example, $ _ FILES ['file']
* @ Param string $ destFolder the directory uploaded, which is automatically created
* @ Param string $ fileTypes File naming method after upload 0 use the original file name 1 use the current timestamp as the file name
* @ Return int
* @ Access public
*/
Public function upload ($ fileField, $ destFolder = './', $ fileNameType = 1 ){
Switch ($ fileField ['error']) {
Case UPLOAD_ERR_ OK: // The value is 0, no error occurs, and the file is uploaded successfully.
$ Upload_succeed = true;
Break;
Case UPLOAD_ERR_INI_SIZE: // The value is 1. the uploaded file exceeds the limit of the upload_max_filesize option in php. ini.
Case UPLOAD_ERR_FORM_SIZE: // The value is 2. The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
$ ErrorMsg = 'File Upload Failed! Cause of failure: the file size exceeds the limit! ';
$ ErrorCode =-103;
$ Upload_succeed = false;
Break;
Case UPLOAD_ERR_PARTIAL: // value: 3; only part of the file is uploaded.
$ ErrorMsg = 'File Upload Failed! Cause of failure: only part of the file is uploaded! ';
$ ErrorCode =-101;
$ Upload_succeed = false;
Break;
Case UPLOAD_ERR_NO_FILE: // value: 4; no file is uploaded.
$ ErrorMsg = 'File Upload Failed! Cause of failure: No file is uploaded! ';
$ ErrorCode =-102;
$ Upload_succeed = false;
Break;
Case UPLOAD_ERR_NO_TMP_DIR: // The value is 6 and the Temporary Folder cannot be found. PHP 4.3.10 and PHP 5.0.3 are introduced.
$ ErrorMsg = 'File Upload Failed! Cause of failure: the Temporary Folder cannot be found! ';
$ ErrorCode =-102;
$ Upload_succeed = false;
Break;
Case UPLOAD_ERR_CANT_WRITE: // The value is 7, and the file write fails. PHP 5.1.0 is introduced.
$ ErrorMsg = 'File Upload Failed! Cause of failure: file writing failed! ';
$ ErrorCode =-102;
$ Upload_succeed = false;
Break;
Default: // other errors
$ ErrorMsg = 'File Upload Failed! Cause of failure: others! ';
$ ErrorCode =-100;
$ Upload_succeed = false;
Break;
}
If ($ upload_succeed ){
If ($ fileField ['SIZE']> $ this-> maxFileSize ){
$ ErrorMsg = 'File Upload Failed! Cause of failure: the file size exceeds the limit! ';
$ ErrorCode =-103;
$ Upload_succeed = false;
}
If ($ upload_succeed ){
$ FileExt = FileSystem: fileExt ($ fileField ['name']);
If (! In_array (strtolower ($ fileExt), $ this-> allowFileTypes )){
$ ErrorMsg = 'File Upload Failed! Cause of failure: the file type is not allowed! ';
$ ErrorCode =-104;
$ Upload_succeed = false;
}
}
}
If ($ upload_succeed ){
If (! Is_dir ($ destFolder) & $ destFolder! = './' & $ DestFolder! = '../'){
$ Dirname = '';
$ Folders = explode ('/', $ destFolder );
Foreach ($ folders as $ folder ){
$ Dirname. = $ folder .'/';
If ($ folder! = ''& $ Folder! = '.' & $ Folder! = '..'&&! Is_dir ($ dirname )){
Mkdir ($ dirname );
}
}
Chmod ($ destfolder, 0777 );
}
Switch ($ fileNameType ){
Case 1:
$ FileName = date ('ymdhis ');
$ Dot = '.';
$ FileFullName = $ fileName. $ dot. $ fileExt;
$ I = 0;
// Determine whether a duplicate file exists
While (is_file ($ destFolder. $ fileFullName )){
$ FileFullName = $ fileName. $ I ++. $ dot. $ fileExt;
}
Break;
Case 2:
$ FileFullName = date ('ymdhis ');
$ I = 0;
// Determine whether a duplicate file exists
While (is_file ($ destFolder. $ fileFullName )){
$ FileFullName = $ fileFullName. $ I ++;
}
Break;
Default:
$ FileFullName = $ fileField ['name'];
Break;
}
// Die ($ destFolder. $ fileFullName );
$ Tempfile = str_replace ('\', '\', $ fileField ['tmp _ name']);
// $ Files =
If (move_uploaded_file ($ tempfile, $ destFolder. $ fileFullName )){
Return $ fileFullName;
} Else {
$ ErrorMsg = $ destFolder. $ fileFullName. "An error occurred while uploading the file! Cause of failure: An error occurred while reading and writing permissions to the local file system! ";
$ ErrorCode =-105;
$ Upload_succeed = false;
}
}
If (! $ Upload_succeed ){
Throw new Exception ($ errorMsg, $ errorCode );
}
}