- Class FileUpload {
- Private $ filepath; // specifies the path to save the uploaded file
- Private $ allowtype = array ('GIF', 'jpg ', 'PNG', 'jpeg '); // specifies the type of the file to be uploaded.
- Private $ maxsize = 1000000; // the maximum allowable file length is 1 MB.
- Private $ israndname = true; // whether to rename randomly. if the parameter is set to false, the original file name is used.
- Private $ originName; // source file name
- Private $ tmpFileName; // temporary file name
- Private $ fileType; // file type
- Private $ fileSize; // file size
- Private $ newFileName; // new file name
- Private $ errorNum = 0; // error code
- Private $ errorMess = ""; // used to provide error reports
-
- // Used for initial conversion of uploaded files
- // 1. specify the upload path, 2, the acceptable type, 3, size limit, 4, whether to use the random file name
- // You do not need to transmit parameters by position. the values provided by the following parameters do not need to provide the values of the previous parameters.
- Function _ construct ($ options = array ()){
- Foreach ($ options as $ key => $ val ){
- $ Key = strtolower ($ key );
- // Check whether the subscript of the array in the user parameter is the same as the member attribute name
- If (! In_array ($ key, get_class_vars (get_class ($ this )))){
- Continue;
- }
- $ This-> setOption ($ key, $ val );
- }
- }
- Private function getError (){
- $ Str = "an error occurred while uploading the file {$ this-> originName :";
- Switch ($ this-> errorNum ){
- Case 4: $ str. = "no file is uploaded"; break;
- Case 3: $ str. = "The file is only partially uploaded"; break;
- Case 2: $ str. = "the uploaded file exceeds the value specified by the MAX_FILE_SIZE option 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. = "Last supported types"; break;
- Case-2: $ str. = "the file to be uploaded is too large to exceed {$ this-> maxSize} bytes"; break;
- Case-3: $ str. = "Upload failed"; break;
- Case-4: $ str. = "an error occurred while creating the Upload file directory. please specify the upload Directory Again"; break;
- Case-5: $ str. = "the path of the file to be uploaded must be specified"; break;
- Default: $ str. = "Unknown error ";
- }
- Return $ str .'
';
- }
- // Used to check the file upload path
- Private function checkFilePath (){
- If (empty ($ this-> filepath )){
- $ This-> setOption ('errornum',-5 );
- Return false;
- }
- If (! File_exists ($ this-> filepath) |! Is_writable ($ this-> filepath )){
- If (! @ Mkdir ($ this-& gt; filepath, 0755 )){
- $ This-> setOption ('errornum',-4 );
- Return false;
- }
- }
- Return true;
- }
- // Used to check the file upload size
- Private function checkFileSize (){
- If ($ this-> fileSize> $ this-> maxsize ){
- $ This-> setOPtion ('errornum', '-2 ');
- Return false;
- } Else {
- Return true;
- }
- }
- // Check the file upload type
- Private function checkFileType (){
- If (in_array (strtolower ($ this-> fileType), $ this-> allowtype )){
- Return true;
- } Else {
- $ This-> setOption ('errornum',-1 );
- Return false;
- }
- }
- // Set the name of the uploaded file
- Private function setNewFileName (){
- If ($ this-> israndname ){
- $ This-> setOption ('newfilename', $ this-> proRandName ());
- } Else {
- $ This-> setOption ('newfilename', $ this-> originName );
- }
- }
-
- // Set the random file name
- Private function proRandName (){
- $ FileName = date ("YmdHis"). rand (100,999 );
- Return $ fileName. '.'. $ this-> fileType;
- }
- Private function setOption ($ key, $ val ){
- $ This-> $ key = $ val;
- }
- // Used to upload a file
- Function uploadFile ($ fileField ){
- $ Return = true;
- // Check the file upload path
- If (! $ This-> checkFilePath ()){
- $ This-> errorMess = $ this-> getError ();
- Return false;
- }
- $ Name = $ _ FILES [$ fileField] ['name'];
- $ Tmp_name = $ _ FILES [$ fileField] ['tmp _ name'];
- $ Size = $ _ FILES [$ fileField] ['size'];
- $ Error = $ _ FILES [$ fileField] ['error'];
- If (is_Array ($ name )){
- $ Errors = array ();
- For ($ I = 0; $ I If ($ this-> setFiles ($ name [$ I], $ tmp_name [$ I], $ size [$ I], $ error [$ I]) {
- If (! $ This-> checkFileSize () |! $ This-> checkFileType ()){
- $ Errors [] = $ this-> getError ();
- $ Return = false;
- }
- } Else {
- $ Error [] = $ this-> getError ();
- $ Return = false;
- }
- If (! $ Return)
- $ This-> setFiles ();
- }
- If ($ return ){
- $ FileNames = array ();
- For ($ I = 0; $ I If ($ this-> setFiles ($ name [$ I], $ tmp_name [$ I], $ size [$ I], $ error [$ I]) {
- $ This-> setNewFileName ();
- If (! $ This-> copyFile ()){
- $ Errors = $ this-> getError ();
- $ Return = false;
- } Else {
- $ FileNames [] = $ this-> newFileName;
- }
- }
- }
- $ This-> newFileName = $ fileNames;
- }
- $ This-> errorMess = $ errors;
- Return $ return;
- } Else {
- 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;
- }
- If (! $ Return)
- $ This-> errorMess = $ this-> getError ();
- Return $ return;
- }
- }
- Private function copyFile (){
- If (! $ This-> errorNum ){
- $ Filepath = rtrim ($ this-> filepath ,'/').'/';
- $ Filepath. = $ this-> newFileName;
- If (@ move_uploaded_file ($ this-> tmpFileName, $ filepath )){
- Return true;
- } Else {
- $ This-> setOption ('errornum',-3 );
- Return false;
- }
- } Else {
- Return false;
- }
- }
- // Set content related to $ _ FILES
- 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 );
- $ ArrStr = explode ('.', $ name );
- $ This-> setOption ('filetype', strtolower ($ arrStr [count ($ arrStr)-1]);
- $ This-> setOption ('filesize', $ size );
- Return true;
- }
- // Get the name of the uploaded file
- Function getNewFileName (){
- Return $ this-> newFileName;
- }
- // If the upload fails, call this method to view the error report.
- Function getErrorMsg (){
- Return $ this-> errorMess;
- }
- }
|