Five-minute PHP Upload class implementation. PHP has many things worth learning. here we will mainly introduce the PHP Upload solution. I hope you will gain a lot through this article. You can directly enter the PHP command on the WEB page on behalf of PHP, which is worth learning. here we will mainly introduce the PHP Upload solution. I hope you will gain a lot through this article. You can directly enter the PHP command code on the WEB page without any special development environment. On the WEB page, all PHP code is placed in. In addition, you can also choose to use the form such. The PHP engine automatically identifies and processes all code on the page between PHP delimiters.
The syntax structure of PHP script is very similar to that of C and Perl. You do not need to declare variables before using them. Using PHP to create an array is also very simple. PHP also has basic object-oriented component functions, which greatly facilitates users to effectively organize and encapsulate their own code. next we will introduce the PHP Upload class in detail.
PHP Upload class implementation code:
- Php
- /**
- * Fileuploadclass
- * @ Version1.0.0 (ThuAug1801: 32: 39CST2005)
- * @ Authorsanshi
- */
- ClassupLoad
- {
- /**
- *
- * @ Authorsanshi
- * @ Version1.0.0ThuAug1801: 00: 18CST2005
- * @ Paramstring $ info file content
- * @ Paramstring $ file name generated by fileName
- * @ Returnboolean returns true if creation is successful.
- * @ Deprecated
- * Create an html file
- */
- FunctioncreateHtml ($ info, $ fileName)
- {
- }
- /**
- *
- * @ Authorsanshi
- * @ Version1.0.0ThuAug1801: 03: 09CST2005
- * @ Returnvoid
- * @ Deprecated
- * Constructor
- */
- FunctiondownLoad ()
- {}
- /**
- *
- * @ Authorsanshi
- * @ Version1.0.0ThuAug1801: 03: 55CST2005
- * @ Paramstring $ field name of fileField in the form
- * @ Paramstring $ length limit
- * @ Returnboolean success return true
- * @ Deprecated
- * Function implementation functions
- */
- Functioninit ($ fileField, $Length='')
- {
- $Files= $ _ FILES [$ fileField];
- // Change the user name based on your actual situation.
- $UserName='Sanshi';
- $FileName= $ Files ['name'];
- $FileType= $ Files ['type'];
- $FileTemp= $ Files ['tmp _ name'];
- $FileSize=Empty($ Length )? ($ Files ['size'] + 10): $ length;
- $FileError= $ Files ['error']; // This part may not exist in php4
- // Change
- // If ($ this->_ IsType ($ fileName) | $ this->_ IsBig ($ length ))
- If (! $ This->_ IsType ($ fileName) | $ this->_ IsBig ($ length) | $ fileError! = 0)
- {
- // Print_r ($ files );
- Returnfalse;
- } Else {
- $Path= $ This->_ CreateDir ($ userName); // Obtain the path
- $CreateFileName= $ UserName. "_". time (); // sets the current file name
- $CreateFileType= $ This->GetFileType ($ fileName); // sets the file category
- Return @ move_uploaded_file ($ fileTemp, $ path. $ createFileName. '.'. $ createFileType )? True: false;
- }
- }
-
- /**
- *
- * @ Authorsanshi
- * @ Version1.0.0ThuAug1801: 07: 43CST2005
- * @ Paramint $ length indicates the maximum upload size.
- * @ Returnboolean returns true if the value exceeds
- * @ Deprecated
- * Determine whether the specified size is exceeded.
- */
- Function_isBig ($ length)
- {
- $Bigest='';
- Return $ big>$ Bigest? True: false;
- }
- /**
- *
- * @ Authorsanshi
- * @ Version1.0.0ThuAug1801: 08: 55CST2005
- * @ Paramstring $ fileName file name
- * @ Returnstring $ fileType file suffix
- * @ Deprecated
- * Get the file suffix (only get the last suffix of the file)
- */
- FunctiongetFileType ($ fileName)
- {
- Returnend (explode ('.', $ fileName ));
- }
- /**
- *
- * @ Authorsanshi
- * @ Version1.0.0ThuAug1801: 10: 41CST2005
- * @ Paramstring $ fileName file name
- * @ Paramboolean $ method whether to check multiple suffixes; default value: false
- * @ Paramint $ the default number of postFix suffixes is 2.
- * @ Returnboolean returns true if Yes
- * @ Deprecated
- * Check whether the file suffix is in the category array. set the category array by yourself.
- * If $ method is set to true, check the file with several suffixes.
- */
- Function_isType ($ fileName, $Method='False', $PostFix=2)
- {
- // Set the category array
- $Type=Array('Jpeg ',
- 'Gif ',
- 'Bmp ',
- 'Exe ');
- $FileName=Strtolower($ FileName );
- $FileTypeArray=Explode('.', $ FileName );
- $FileType=End($ FileTypeArray );
- // Determine whether a file has multiple suffixes
- If ($ method)
- {
- If (count ($ fileTypeArray)>(Is_int ($ postFix )? $ PostFix: 2 ))
- {
- Returnfalse;
- }
- }
- Returnin_array ($ fileType, $ type );
- }
-
- /**
- *
- * @ Authorsanshi
- * @ Version1.0.0ThuAug1801: 17: 19CST2005
- * @ Paramstring $ userName
- * @ Returnstring $ path
- * @ Deprecated
- * Create a directory in the format of year/month/day/user name/
- * The permission is 755.
- */
- Function_createDir ($ userName)
- {
- $Root='';
- $PathSign=DIRECTORY_SEPARATOR;
- $Y=Date('Y'). $ pathSign;
- $M=Date('M'). $ pathSign;
- $D=Date('D'). $ pathSign;
- $Path= $ Root. $ y. $ m. $ d. $ userName;
- $DirArray=Explode($ PathSign, $ path );
- $TempDir='';
- Foreach ($ dirArrayas $ dir)
- {
- $TempDir.= $ Dir. $ pathSign;
- $IsFile=File_exists($ TempDir );
- Clearstatcache ();
- If (! $ IsFile &&! Is_dir ($ tempDir ))
- {
- @ Mkdir ($ tempDir, 0755 );
- }
- }
- Return $ path. $ pathSign;
- }
- /**
- *
- * @ Authorsanshi
- * @ Version1.0.0ThuAug1801: 19: 32CST2005
- * @ Param string $ dirName directory name
- * @ Return boolean the return value is true.
- * @ Deprecated
- * Determine whether the operation is in the upload Directory
- */
- Function_isDel ($ dirName)
- {
- // Pay attention to upLoadDir, which must correspond to the actual directory used
- $UpLoadDir='';
- $UpLoadDir=Preg_replace('/\ //', '/', $ UpLoadDir );
- $Format="/^ {$ UpLoadDir }/";
- Returnpreg_match ($ format, $ dirName );
- }
- /**
- *
- * @ Authorsanshi
- * @ Version1.0.0ThuAug1801: 25: 58CST2005
- * @ Paramstring $ fileName file name
- * @ Returnboolean returns true if the object is successfully deleted.
- * @ Deprecated
- * Delete an object
- */
- FunctiondelFile ($ fileName)
- {
- $Cur_dir=Dirname(Trim ($ fileName ));
- If ($ this->_ IsDel ($ cur_dir ))
- {
- Return @ unlink ($ fileName )? True: false;
- } Else {
- Returnfalse;
- }
- }
- /**
- *
- * @ Authorsanshi
- * @ Version1.0.0ThuAug1801: 27: 43CST2005
- * @ Paramstring $ dieName directory name
- * @ Returnboolean return true if deletion is successful
- * @ Deprecated
- * Deleting files in the directory cannot be deleted.
- */
- FunctiondelDir ($ dirName)
- {
- If ($ this->_ IsDel ($ dirName) & is_dir ($ dirName ))
- {
- Return @ rmdir ($ dirName )? True: false;
- } Else {
- Returnfalse;
- }
- }
-
- }
- ?>
- Php
- // Use
- /*
- Include 'upload. class. php ';
- $Up=NewupLoad();
- If ($ up->Init ("file "))
- {
- Echo 'success ';
- } Else {
- Echo 'failure ';
- }
- */
- ?>
Bytes. You can directly enter the PHP command generation on the WEB page...