if (! defined ("class_gggfileupload") {define ("class_gggfileupload", "1");
Class GggfileUPload {var $ missbug = false; // Setting the debug status, debugging in the class will be wrong, do not debug false true var $ mstrer = "in the class, if ISDEBUG is true, then Automatically abort the program execution after the error, output error message VAR $ miserr = false; // Upload whether an error status flag
VAR $ MISFILHT = true; // Ture will ensure that the file names after the upload can only be made of letters, numbers, upper and downlines, and points; // Ture will use lowercase, due to Win and UNIX The system is sensitive to the file name size, and there will be a lot of troubles in the download file. Var $ mmaxfilenamelen = 30; // The maximum length allowed by file names
VAR $ extfilter = array ('php' => 'php_'); // Set the extension of the extension here, such as the user uploaded .php file is changed to .PHP_ program uses a replacement method such as .php3 Changed to .php_3
Var $ minputname; // The corresponding AAA, * saved a character
VAR $ mfiletype; // Upload type VAR $ mfilesize; // Upload size VAR $ mfilename; // Upload name var $ mfileTmpname; // Uploaded temporary name
VAR $ mfileuppath; // File To Upload Server Address Var $ mfileUpType; // File Upload Allow Type Var $ mfileupSize; // File Upload The Maximum Byte Var $ mfileupname; // Upload the name, That is to change, the name var $ mrorfile = "failt"; // file name is wrong to change the file name starting in MERRORFILE
/ ** ---------------------------------------------- - Function Name: GggfileUPload Features Description: GggfileUPload Initialization Function Description: Class constructor used for initialization class member variable call function: gggfileUpload () Parameters: $ inputname Corresponding AAA, saved Is a character strike $ FILEUPPATH file To Upload Server Address $ FILEUPTYPE File Upload Allow Type $ FileUpsize file Upload Allow Maximum Bytes Default 15000 bytes Back: No Design: GGG Date: 2004-9-1 1 : 17 Modifier: GGG Date: 2004-9-1 1:17 version: 1.0 ** --------------------------- -------------------- * / Function GGGFileUpload ($ INPUTNAME, $ FILEUPPATH, $ fileupType = ", $ fileupsize = 150000) {$ this-> set_file ($ inputName); $ this-> Set_FileUpPath ($ fileUpPath); $ this-> Set_FileUpSize ($ fileUpSize); $ this-> Set_FileUpType ($ fileUpType);} // END GggFileUpload function Show_Debug () {// if ($ this- > MISDEBUG &&! EMPTY ($ this-> get_strer ())))) f ($ this-> MISDEBUG &&! EMPTY ($ this-> mstrerr)) {printf ("
Debug: % S font>
/ n ", $ this-> get_strerr ());}} // ******************** SET area / / Setup error variable Function set_strerr ($ VarName) {$ this-> miserr = Tru E; if (! EMPTY ($ VarName)) $ this-> mstrer = trim ($ varname);
// Setting the Properties of the file Function set_file ($ INPUTNAME) {if (! Empty ($ INPUTNAME)) {$ this-> minputname = $ inputname; // Corresponding AAA, * Save Is a character, $ this-> mfiletype = $ _ files [$ inputname] ["type"]; // file upload type $ this-> mfilesize = $ _ files [$ inputname] ["size"]; // file Uploaded size $ this-> mfilename = $ _ files [$ inputname] ["name"]; // File uploaded name $ this-> mfiletmpname = $ _ files [$ inputname] ["tmp_name"]; // File upload Temporary name}}}
// Set file file name Function set_filename ($ filename) {if (! Empty ($ filename)) $ this-> mfilename = trim ($ filename);} // setup file To upload server address function set_fileuppath ($ fileuppath) { IF (Strlen ($ fileuppath) && substr ($ fileuppath, -1)! = '/') {$ fileuppath. = '/';} $ this-> mfileuppath = trim ($ fileuppath);
/ / Setting up the maximum byte of the file uploaded Function set_fileupsize ($ fileupsize) {if (! Empty ($ fileupsize) && EREG ("^ [1-9] [0-9] * $", $ fileupsize)) $ This-> mfileupsize = $ fileupsize;}
// Setting the file upload Allow type Function set_fileupty ($ fileupType) {if (! EMPTY ($ fileupType)) $ this-> mfileUpType = TRIM ($ fileupType); // Upload the type Allow
// ********************* Get area
// Get file name Function get_filename () {return $ this-> mfilename;
// Get the type of file function get_filetype () {return $ this-> mfiletype;
// Get the size of the file Function get_filesize () {return $ this-> mfilesize;}
// Temporary Function get_filetmpname () {Return $ this-> mfileTmpname;}
// Get the file to upload server address function get_fileuppath () {return $ this-> mfileuppath;}
// Get the maximum byte allowed by the file upload fullupsize () {Return $ this-> mfileupsize;}
// Get the type of file uploads allowed by file up_fileuptype () {return $ this-> mfileuptype;} // Get error code FUNCTION GET_STRERR () {Return $ this-> mstrerr;}
// ********************* CHECK area
/ ** ---------------------------------------------- - Function Name: Check_File function Description: Check the legal function of the file: Call_file () Parameters: Check_File () Parameters: Designer: GGG Date: 2004-9-1 7:06 Modifier: GGG Date: 2004-9- 1 7:06 version: 1.0 ** --------------------------------------- -------- * /
Function check_file () {$ file_tmp_name = $ this-> get_filetmpname (); // get temporary file name $ file_name = $ this-> get_filename (); // Get file name $ file_size = $ this-> get_filesize (); / / Get file size $ file_up_size = $ this-> get_fileupsize (); // Get file upload size // Check if the uploaded file exists if (Empty ($ file_tmp_name) | | $ file_tmp_name == "none" ||! File_exists $ FILE_TMP_NAME) || Empty ($ file_size)) // Do not select the file you want to upload, return {$ this-> set_strerr ("Uploaded files do not exist!"); $ this-> show_debug (); return false;} If (! is_writeable ($ this-> get_fileuppath ()) // If you uploaded to the directory can be written {$ this-> set_strerr ("Upload Directory:". $ this-> get_fileuppath (). "Does not exist or not Write, please change the directory properties first! "); $ This-> show_debug (); return false;} // Verify the legitimacy IF of the file name of the upload file ($ file_size> $ file_up_size) / / Change to lowercase {$ this -> set_strerr ("The maximum size allowed by the upload file is". $ File_up_size. "Bytes !!"); $ this-> show_debug (); return false;}
// Verify that the file type is correct if (false == $ this-> check_fileupty ()) {return false;}
/ / Verify the legitimacy of the file name of the upload file IF ($ this-> misfilelower) / / change to lowercase $ file_name = $ this-> MySTRTOLOWER ($ file_name);
// Get file names and extensions $ file_name_name = trim ($ file_name, '.')); $ File_name_ext = trim ($ this-> get_str_end ($ file_name, '.');
IF ($ this-> misfileright) // If you want to ensure that the file name can only be composed of letters, numbers, upper and lower lines, point numbers {if (! EREG ("^ [0-9a-z /._/-] ", $ file_name_ext) // Look at the legality of the extension {$ this-> set_strerr (" Upload extension: ". $ file_name_ext." Do not meet the standard, remove the extension "); $ FILE_NAME_EXT =" " // The processing here is to delete the extension $ this-> show_debug ();} if (! EREG ("^ [0-9a-z /._/-] (", $ file_name_name)) // The legality {// srand ((Double) microtime () * 1000000); $ this-> set_strerr ("Uploaded file name:". $ File_name_name. "Does not meet the standard"); $ file_name_name = Time (). $ this-> mrrorfile; //$file_name_name=rand (1,999). $ 10isystemfile; $ this-> mstrerr. = ", has been changed:" $ file_name_name. "!"; $ this-> show_debug );} $ File_name = $ file_name_name. $ File_name_ext; // Recent new file name} // File name too long process IF (Strlen ($ file_name)> $ this-> mmaxfileenamelen) // file name too long {IF (Strlen ($ file_name_ext)> $ this-> mmaxfilenamelen) // If the extension is greater than the maximum character of the allowed character, the maximum character may be small {$ this-> set_strerr ("extension length is greater than:". $ this-> mmaxfilenamelen. "!"); $ File_name_ext = Substr ($ file_name_name, 0, 5); $ this-> show_debug ();} if (Strlen ($ file_name_name)> $ this-> mmaxfilenamelen) // If the file The length that is greater than the longest character allowed, also the length of the {// file name is based on the length of the total length reduction extension, $ this-> set_strerr ("file name length is greater than:". $ This-> mmaxfilenamelen. "! "); $ File_name_name = Substr ($ file_name_name, 0, $ this-> mmaxfilenamelen-strlen ($ file_name_ext)); $ this-> show_debug ();}}
// Filter the extension Check while (List ($ K, $ V) = Each ($ this-> extfilter) {if (strstr ($ file_name_ext, $ k)) // extension contains keyword { $ this-> mstrerr. = "The extension contains keywords:" $ k. "!"; $ file_name_ext = STR_REPLACE ($ K, $ V, $ FILE_NAME_EXT); $ this-> show_debug ();}} $ FILE_NAME = $ file_name_name. $ file_name_ext; // Request to new file name // This time you have got the final file name $ this-> set_filename ($ file_name); // Reset the file name of the class member variable Return! false } // end check_file
/ ** ---------------------------------------------- - Function Name: Check_File_EXITS Function Description: Check if the upload file file has existed: CHECK_FILE_EXITS (FileName) Parameters: Return: Return File Name Designer: GGG Date: 2004-9-1 21:45 Modified: GGG Date: 2004-9-1 21:45 version: 1.0 ** ----------------------------- ------------------ * /
function Check_File_Exits ($ fileName) {// get the file name and extension name $ file_name_name = trim ($ this-> Get_Str_Head ($ fileName,) '.'); $ file_name_ext = trim ($ this-> Get_Str_End ($ fileName, ' . ')); // Watch the file to be uploaded, modify the file name IF (file_exists ($ this-> get_fileuppath (). $ Filename)) {$ this-> mstrerr. = "To upload the file The existence: "$ this-> get_fileuppath (). $ Filename."! "; $ File_name_name = substr ($ file_name_name, 0, $ this-> mmaxfilenamelen); // To prevent continuous repetition file names have become long, this truncation $ count_i = 0; while (file_exists ($ this-> Get_FileUpPath () $ file_name_name $ count_i $ file_name_ext)...) $ count_i ;. // a1 a2 a3 $ fileName = $ file_name_name $ count_i. $ file_name_ext; // Recent new file name} Return $ filename;} / ** ----------------------------- ------------------ Function Name: Check_FileUpType Features Description: Check the file upload Allow type function Description: Direct call_fileUPType () parameter: return: Return True Or False, indicating whether it is a successful designer: GGG Date: 2004-9-1 7:04 Modifier: GGG Date: 2004-9-1 7:04 version: 1.0 ** ---------- --------------------------------------- * / function check_fileupType () {$ file_up_type = $ This-> get_fileuptype (); if (EMPT Y ($ file_up_type)) // If the file type is not specified, it means all types of type {return! false;}
$ FILE_TYPE = $ this-> get_filetype (); $ array_filetype = exPlode (",", $ file_up_type); // Judgment the file type is compliant, separating for ($ count_i = 0; $ count_i
Function Upload ($ Keepname = ", $ OriginalFile =" ") {i (! EMPTY ($ Keepname)) {$ this-> set_filename ($ Keepname); // Change the uploaded file name $ THIS-> set_strerr (" Change the uploaded file name 1: "$ this-> get_filename ()); $ this-> show_debug ();} // check the uploaded file size type, etc., and rename the problematic file name .. if (false = = $ THIS-> Check_File ()) {// $ this-> set_strerr ("Check the uploaded file has problems: check_file"); // $ this-> show_debug (); if (! Empty ($ OriginalFile) Return $ othernalFile; Else Return "";} // Remove the original file editing status Use if (! EMPTY ($ OriginalFile) && file_exists ($ this-> get_fileuppath (). $ originalfile)) {@unlink ($ this-> GET_FILEUPPATH (). $ OriginalFile; // Delete OriginalFile file $ this-> set_strerr ("deleted:". $ This-> get_fileuppath (). $ OriginalFile. "File"); $ this-> show_debug (); } $ This-> set_filename ($ this-> get_file_exits ($ this-> get_filename ()))); // Check if the file exists, generate a new file name / * $ this-> set_file ($ inputname); $ THIS-> Set_fileuppath; $ this-> set_fileupsize ($ fileupsize); $ this-> set_fileupType ($ fileupType); Print ($ this-> minputname. "
"); Print ($ this-> mfileuptype. "
"); Prin T ($ this-> mfileupsize. "
"); Print ($ this-> mfileuppath. "
"); Print ($ this-> mfilety. "
"); print ($ this- > mfilesize. "
"); Print ($ this-> mfilename. "
"); Print ($ this-> mfiletmpname. "
"); * /
if (is_uploaded_file ($ this-> Get_FileTmpName ())) {@move_uploaded_file ($ this-> Get_FileTmpName (), $ this-> Get_FileUpPath () $ this-> Get_FileName ().);} else {$ this-> Set_StrErr ("Checking the uploaded file has a problem:" $ this-> get_filetmpname (). "Is_uploaded_file"); $ this-> show_debug (); return ";} / * Even if the copy success is also returned. I don't know what IF ($ this-> get_filetmpname (), $ this-> get_fileuppath (). $ This-> get_filename ()) == false); // Upload file {$ this-> set_strerr (" Copy the file: "$ this-> get_fileuppath (). $ This-> get_filename ()." Failure "); $ this-> show_debug (); return";} * @ @ unlink ($ this-> Get_filetmpname ()); // Delete Temporary File Return $ this-> get_filename ();}
/ * Get the character header with a character interval, the tail * / function get_str_head ($ VarName, $ SEPARATOR = '.') // Returns the name {// print ($ VarName); // Die (); $ file_len = 0; if (! ($ File_len = Strrpos ($ VarName, $ SEPARATOR))) // If the file has no extension, the file name is all file {$ file_len = Strlen ($ VarName);} $ file_name = Substr $ VarName, 0, $ file_len; // Get the upload file name Return (Substr ($ VarName, 0, $ file_len);} // get_str_head ()
Function get_str_end ($ VARNAME, $ SEPARATOR = '.') // Returns the extension of the file {$ file_len = 0; $ file_ext = ""; if ($ file_len = STRPOS ($ VARNAME, $ SEPARATOR)) // If the file Without the extension, the file name is all file, extension, empty {$ file_ext = substr ($ VARNAME, - (Strlen ($ VARNAME) - $ file_len)); // Get the extension of the upload file} Return ($ file_ext) } // get_str_end ()
// Customize the lowercase in the strings into uppercase, but does not destroy the Chinese word Function MySTRTOLOWER ($ VARNAME) {Return preg_replace ('/ [AZ] / e', "Strtolower ('// 0')" , $ Varname);
} // End class gggfileupload}
// ******************** class test // ******************** Test // ******************** class test
/ *
IF ("Submit" == $ _ post ["Command"]) {// allows multiple files to be uploaded with Text, Image, Application with comma intervals $ t = new gggfileupload ("fileename", "../ TEST / "," text, image "); // If you do not specify the allowed file type. The default is all files // $ t-> Upload ("ggg.txt", "ggg.txt"); // Delete the ggg.txt file before the file, then change the uploaded file name to GGG.txt $ NEW_FILE_NAME = $ T-> UPLOAD (); // Direct the file to the online IF (Empty ($ new_file_name)) // new_file_name is empty definitely the file upload failed {print ("file upload failed");} else {print ("Document Upload Success");} Print ($ T-> Get_FileName ()); // $ t = new gggfileupload ("filename", "../ Test /", "text, image"); // $ T-> Upload ($ keEpName = "ggg.txt", $ originalfile = "ggg.txt"); // Realize the same name override
// $ t-> set_file ("FileName1", "../ Test /", "text, image"); // Set the second file upload parameter // $ t-> UPLOAD (); // The upload of the second file.
// $ t-> set_file ("filename2"); // Set the third file upload parameter // $ T-> UPLOAD (); // Upload of the third file.
} else {// $ str = "Use C C extended php.html"; // $ str = preg_replace ('/ [az] / e', "Strtolower ('// 0')", $ STR ); // $ str = preg_replace ('/ [/ x0- / x9f] [AZ] / e', "Strtolower ('// 0')", $ STR); ECHO $ Str; Print ("