/*
- * Class: File Upload class
- * Author: ZMM
- * Date: 2011.1.20
- * Email: 304924248@qq.com
- * Link: http://bbs.it-home.org
- */
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 (){}
- }
- ?>
|