<? Php Class upload { Public $ up_ext = array (), $ up_max = 5210, $ up_dir; Private $ up_name, $ up_rename = true, $ up_num = 0, $ up_files = array (), $ up_ret = array (); Function _ construct ($ name, $ ext = array (), $ rename = true ){ If (! Empty ($ name )){ $ This-> up_name = $ name; ! Empty ($ ext) & $ this-> up_ext = $ ext; $ This-> up_rename = $ rename; $ This-> up_dir = website_dirroot. $ Globals ['cfg _ upload_path ']; $ This-> initupload (); } Else { Exit ('upload file domain name is empty, initialization failed! '); } } Private function initupload (){ If (is_array ($ _ files [$ this-> up_name]) { $ Up_arr = count ($ _ files [$ this-> up_name]); $ Up_all = count ($ _ files [$ this-> up_name], 1 ); $ Up_cnt = ($ up_all-$ up_arr)/$ up_arr; For ($ I = 0; $ I <$ up_cnt; $ I ++ ){ If ($ _ files [$ this-> up_name] ['error'] [$ I]! = 4 ){ $ This-> up_files [] = array ( 'Tmp _ name' => $ _ files [$ this-> up_name] ['tmp _ name'] [$ I], 'Name' => $ _ files [$ this-> up_name] ['name'] [$ I], 'Type' = >$ _ files [$ this-> up_name] ['type'] [$ I], 'SIZE' = >$ _ files [$ this-> up_name] ['SIZE'] [$ I], 'Error' => $ _ files [$ this-> up_name] ['error'] [$ I] ); } } $ This-> up_num = count ($ this-> up_files ); } Else { If (isset ($ _ files [$ this-> up_name]) { $ This-> up_files = array ( 'Tmp _ name' => $ _ files [$ this-> up_name] ['tmp _ name'], 'Name' => $ _ files [$ this-> up_name] ['name'], 'Type' = >$ _ files [$ this-> up_name] ['type'], 'SIZE' = >$ _ files [$ this-> up_name] ['SIZE'], 'Error' => $ _ files [$ this-> up_name] ['error'] ); $ This-> up_num = 1; } Else { Exit ('the file for upload is not found! '); } } $ This-> chkupload (); } Private function chkupload (){ If (empty ($ this-> up_ext )){ $ Up_mime = array ('image/wbmp ', 'image/bmp', 'image/gif', 'image/pjpeg ', 'image/x-png '); Foreach ($ this-> up_files as $ up_file ){ $ Up_allw = false; Foreach ($ up_mime as $ mime ){ If ($ up_file ['type'] = $ mime ){ $ Up_allw = true; break; } } ! $ Up_allw & exit ('upload not allowed '. $ up_file ['type'].' format file! '); If ($ up_file ['SIZE']/1024> $ this-> up_max ){ Exit ('cannot upload files larger than'. $ this-> up_max. 'k! '); } } } Else { Foreach ($ this-> up_files as $ up_file ){ $ Up_ext = end (explode ('.', $ up_file ['name']); $ Up_allw = false; Foreach ($ this-> up_ext as $ ext ){ If ($ up_ext = $ ext ){ $ Up_allw = true; break; } } ! $ Up_allw & exit ('upload is not allowed. '. $ up_ext.' format file! '); If ($ up_file ['SIZE']/1024> $ this-> up_max ){ Exit ('cannot upload files larger than'. $ this-> up_max. 'k! '); } } } $ This-> uploading (); } Private function uploading (){ If (io: dircreate ($ this-> up_dir )){ If (chmod ($ this-> up_dir, 0777 )){ If (! Empty ($ this-> up_files )){ Foreach ($ this-> up_files as $ up_file ){ If (is_uploaded_file ($ up_file ['tmp _ name']) { $ File_name = $ up_file ['name']; If ($ this-> up_rename ){ $ File_ext = end (explode ('.', $ file_name )); $ File_rnd = substr (md5 (uniqid (), mt_rand (0, 26), 6 ); $ File_name = date ('ymdhis ').' _ '. $ file_rnd.'. '. $ file_ext; } $ File_name = $ this-> up_dir. '/'. $ file_name; If (move_uploaded_file ($ up_file ['tmp _ name'], $ file_name )){ $ This-> up_ret [] = str_replace (website_dirroot, '', $ file_name ); } Else { Exit ('file Upload Failed! '); } } } } } Else { Exit ('Write Permission not enabled! '); } } Else { Exit ('upload Directory creation failed! '); } } Public function getupload (){ Return empty ($ this-> up_ret )? False: $ this-> up_ret; } Function _ destruct (){} } ?> |