The specific parsing of PHP Generic file Upload class. For example, if you can upload an exhibit and upload a thumbnail for it, the restricted type of the thumbnail file may be jpg, gif, and png, the following is a detailed explanation.For example, if you can upload an exhibit and upload a thumbnail for it, the restricted type of the thumbnail file may be jpg, gif, and png, the maximum types of exhibits may be mov, avi, mpeg, and so on, while the image size may be limited to kB, and the audio video size may be limited to 2 MB. The class code is as follows:
- Class Upload
- {
- Public $ InputName; // control name of the file upload domain
-
- /**
- * File types that can be uploaded
- * The format is array ('image/jpeg ', 'image/png', 'image/GIF') or an array containing this array (corresponding to each Upload field control)
- */
- Public $ FileType;
-
- /**
- * Maximum size of uploaded files (unit: byte)
- * Format: array ('image' =>$ Size, 'Audio' =>$ Size) (indicates the upload size corresponding to each application file type) or an array containing this array (corresponding to each Upload field control) or a value (indicating that all uploaded files are limited to this size)
- */
- Public $ FileMaxSize;
-
- Public $ FileSavePath; // file storage path (which can be an array, indicating that files are uploaded to different paths in different Upload domains)
- Public $ FileSaveName; // file storage name (excluding the extension name) (which can be an array, indicating different names of uploaded files stored in different Upload domains)
- Public $ NoteFileFalse; // file error prompt
- Public $ NoteFileType; // file type mismatch prompt
- Public $ NoteFileSize; // the error code returned when the file size exceeds the specified value.
-
- /* Upload the file and return the file name information (including the suffix )*/
- Public function UploadFile ()
- {
- $ This->CheckFile (); // check the file
- $File= $ _ FILES [$ this->InputName];
- $File_save_full_name=Array(); // File storage name (including suffix)
-
- Foreach ($ file ['name'] as $Key=>$ Name)
- {
- If (! Empty ($ name) // The file is not empty.
- {
- /* Determine the file storage path */
- If (is_array ($ this->FileSavePath ))
- {
- $File_save_path= $ This->FileSavePath [$ key];
- }
- Else
- {
- $File_save_path= $ This->FileSavePath;
- }
-
- /* Determine the file storage name (excluding the extension name )*/
- If (is_array ($ this->FileSaveName ))
- {
- $File_save_name= $ This->FileSaveName [$ key];
- }
- Else
- {
- $File_save_name= $ This->FileSaveName;
- }
-
- /* Start to save */
- $ This->CreatePath ($ file_save_path); // create a path if the path does not exist
- Move_uploaded_file ($ file ["tmp_name"] [$ key], $ file_save_path. $ file_save_name. $ this->GetSuffix ($ file ['name'] [$ key]);
- $ File_save_full_name [] = $ file_save_name. $ this->GetSuffix ($ file ['name'] [$ key]);
- }
- Else
- {
- $ File_save_full_name [] = null;
- }
- }
-
- Unlink ($ file );
-
- /* If there is only one file, a single file name is returned */
- If (count ($ file_save_full_name) = 1)
- {
- $File_save_full_name= $ File_save_full_name [0];
- }
-
- Return $ file_save_full_name;
- }
-
- /* Inspection File */
- Private function CheckFile ()
- {
- $File= $ _ FILES [$ this->InputName];
-
- Foreach ($ file ['name'] as $Key=>$ Name)
- {
- If (! Empty ($ name) // The file is not empty.
- {
- $Type= $ File ['type'] [$ key];
- $Size= $ File ['size'] [$ key];
- $Error= $ File ['error'] [$ key];
-
- /* Confirm the list of file types allowed to be uploaded */
- If (is_array ($ this->FileType [0])
- {
- $File_type= $ This->FileType [$ key];
- }
- Else
- {
- $File_type= $ This->FileType;
- }
-
- /* Determine the maximum size of the uploaded file */
- If (is_array ($ this->FileMaxSize ))
- {
- $File_max_size_key=Explode('/', $ Type );
- $File_max_size_key= $ File_max_size_key [0];
- If (is_array ($ this->FileMaxSize [0])
- {
- $File_max_size= $ This->FileMaxSize [$ key] [$ file_max_size_key];
- }
- Else
- {
- $File_max_size= $ This->FileMaxSize [$ file_max_size_key];
- }
- }
- Else
- {
- $File_max_size= $ This->FileMaxSize;
- }
-
- /* File error */
- If ($ error>0)
- {
- Die ($ name. $ this->NoteFileFalse );
- }
-
- /* The file size exceeds the maximum size of the uploaded file */
- If ((! Is_null ($ file_max_size) & $ size>$ File_max_size) | ($Size= 0 ))
- {
- Die ($ name. $ this->NoteFileSize );
- }
-
- /* The file type does not match */
- If (! In_array ($ type, $ file_type ))
- {
- Die ($ name. $ this->NoteFileType );
- }
- }
- }
- }
-
- /* Get the file suffix */
- Private function GetSuffix ($ fileName)
- {
- Return substr ($ fileName, strrpos ($ fileName ,"."));
- }
-
- /* Create a path if the path does not exist */
- Private function CreatePath ($ filePath)
- {
- If (! File_exists ($ filePath ))
- {
- Mkdir ($ filePath );
- }
- }
- }
Usage of the PHP Generic file Upload class: Next we will illustrate the calling method of this class using the example given at the beginning of this article (haha, the call is very convenient ):
$ Upload_obj = new Upload (); // object to be uploaded
$ Upload_obj-> InputName = 'upload _ test'; // control name of the file upload domain
$ Upload_obj-> FileType = array ('image/jpeg ', 'image/png'), array ('audio/mpeg ', 'Video/x-msvideo'); // specifies the file type that can be uploaded.
$ Upload_obj-> FileMaxSize = array ('image' => 100*1024, 'Audio' => 2*1024*1024, 'video' => 2*1024*1024 );
$ Upload_obj-> FileSavePath = array ('upload/files/s/', 'upload/files/z /');
$ Upload_obj-> FileSaveName = time ();
$ Upload_obj-> NoteFileFalse = 'File error ';
$ Upload_obj-> NoteFileType = 'File type mismatch ';
$ Upload_obj-> NoteFileSize = 'File size exceeded ';
$ File_save_full_name = $ upload_obj-> UploadFile (); // upload and obtain the full name of the file (basic name and extension) (if multiple files are uploaded, they are in the array format) (The full name is used to store information in the database)
Summary: This allows you to easily upload several files. In fact, the PHP Generic file Upload class uses the PHP Group file upload. Note that after the control name is added, do not forget to add []. the advantage is that when multiple files are uploaded, they do not need to be cyclically or upload one by one at the call layer. Therefore, our applications are relaxed.
For example, if you can upload an exhibit and upload a thumbnail for it, the restricted type of the thumbnail file may be jpg, gif, png, etc ,...