We'll give you a detailed explanation below.For example: A user can upload an exhibit and upload a thumbnail for the exhibit, then the thumbnail file may have a limit type of jpg,,gif,png and so on, while the exhibit file limit type may be mov,avi,mpeg, and the image size may be limited to 100KB. The audio video size may be limited to 2MB. The class code is as follows:
- Class Upload
- {
- Public $InputName; File Upload domain control name
-
- /**
- * File types allowed for uploading
- * The form is array (' image/jpeg ', ' image/png ', ' image/gif ') or an array containing such an array (corresponding to each upload domain control)
- */
- Public $FileType;
-
- /**
- * Maximum upload file size (in bytes)
- * format is an array (' image ' => $size, ' audio ' => $size) (which indicates the upload size for each application file type) or contains this An array of class arrays (corresponding to each upload domain control) or a numeric value (indicating that all uploaded files are limited to this size)
- */
- Public $FileMaxSize;
-
- Public $FileSavePath; File save path (available as an array, representing different upload domains uploading files to different paths)
- Public $FileSaveName; File Save name (does not include suffix) (can be an array, indicating different upload domain upload files to save different names)
- Public $NoteFileFalse; File Error prompt
- Public $NoteFileType; File type mismatch prompt
- Public $NoteFileSize; File size exceeded hint
-
- /* Upload the file and return the filename information (with the suffix name) */
- Public Function UploadFile ()
- {
- $this- > checkfile ();//Inspection file
- $ file = $_files[$this->inputname];
- $ File_save_full_name = Array ();//File Save name (with suffix name)
-
- foreach ($file [' name '] as $key => $name)
- {
- if (!empty ($name))//file is not empty
- {
- /* Determine the File save path */
- if (Is_array ($this->filesavepath))
- {
- $ File_save_path = $this- > filesavepath[$key];
- }
- Else
- {
- $ File_save_path = $this- > Filesavepath;
- }
-
- /* Determine file Save name (without suffix) */
- if (Is_array ($this->filesavename))
- {
- $ File_save_name = $this- > filesavename[$key];
- }
- Else
- {
- $ File_save_name = $this- > Filesavename;
- }
-
- /* Start saving */
- $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 filename is returned */
- if (count ($file _save_full_name) = = 1)
- {
- $ File_save_full_name = $file _save_full_name[0];
- }
-
- return $file _save_full_name;
- }
-
- /* Test File */
- Private Function Checkfile ()
- {
- $ file = $_files[$this->inputname];
-
- foreach ($file [' name '] as $key => $name)
- {
- if (!empty ($name))//file is not empty
- {
- $ type = $file [' type '] [$key];
- $ size = $file [' Size '] [$key];
- $ Error = $file [' ERROR '] [$key];
-
- /* OK to allow uploading of file type list */
- if (Is_array ($this->filetype[0) )
- {
- $ File_type = $this- > filetype[$key];
- }
- Else
- {
- $ File_type = $this- > FileType;
- }
-
- /* Determine the maximum upload file size */
- 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);
- }
-
- /* File size exceeds maximum upload file size */
- if (!is_null ($file _max_size) && $size > $file _max_size) | | ($ size = = 0))
- {
- Die ($name. $this->notefilesize);
- }
- /* 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);
- }
- }
- }
PHP common File Upload class use: Then the example at the beginning of this article to illustrate the invocation method of the class (hehe, the call is very convenient):
$upload _obj = new Upload (); File Upload Object
$upload _obj->inputname = ' upload_test '; File Upload domain control name
$upload _obj->filetype = Array (' Image/jpeg ', ' image/png '), Array (' Audio/mpeg ', ' Video/x-msvideo ')); Allowed file types to upload
$upload _obj->filemaxsize = Array (' image ' = + * 1024x768, ' audio ' = 2 * 1024x768 * 1024x768, ' 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 does not match ';
$upload _obj->notefilesize = ' file size exceeded ';
$file _save_full_name = $upload _obj->uploadfile (); Upload and get full file name (base name plus extension) (if multiple files are in array form) (full name for storing information in database)
Summary: In this can be easily implemented a number of file upload, in fact, PHP general file upload class in the final analysis of the PHP group file upload, to note that the name of the control is not forgotten to add the [], the advantage is to encounter multiple file uploads do not have to cycle in the call layer or a processing upload, Our applications are also easy to use.
http://www.bkjia.com/PHPjc/446390.html www.bkjia.com true http://www.bkjia.com/PHPjc/446390.html techarticle we will explain the following for you, for example: Users can upload an exhibit and upload a thumbnail for the exhibit, then the thumbnail file limit type may be jpg,,gif,png, etc. ..