The upload class uses the Net.updatefile class in the Org Class library package, and the latest version of the upload class contains the following features (some features need to be combined with thinkphp system other class libraries):
1. Basically transmit function
2. Support Batch Upload
3. Support to generate thumbnail images
4. Custom parameter Upload
5. Upload detection (including size, suffix and type)
6. Support upload type, attachment size, upload path definition
7. Support hash or date subdirectory save upload file
8. Upload the image security detection
9. Support upload file naming rules
10. Support for hash verification of uploaded files
Using the upload feature in thinkphp eliminates the need for special processing. For example, here is a form submission with an attachment upload:
The code is as follows |
Copy Code |
<form method=post action= "/manual/upload" enctype= "Multipart/form-data" > <input type= "text" name= "NAME" > <input type= "text" name= "email" > <input type= "file" name= "Photo" > <input type= "Submit" value= "Save" > </form>
|
Note that you must add Enctype= "Multipart/form-data" files to your form label to upload. Because the form is submitted to the upload action method of the current module, we add the following upload method to the module class:
code is as follows |
copy code |
public function upload () { Import (" ORG.Net.UploadFile "); $upload = new UploadFile ();//Instantiate upload class $upload->maxsize = 3145728;//Set attachment upload size $upload->allowexts = arr ay (' jpg ', ' gif ', ' PNG ', ' jpeg '); Set attachment upload type $upload->savepath = './public/uploads/';//Set Attachment upload directory if (! $upload->upload ()) {//Upload error message error messages $this->error ($upload->geterrormsg ()); }else{//upload successfully upload file information $info = $upload->getuploadfileinfo (); } //Save form data including attachment data $User = M ("user");//Instantiate User object $User->create ();//Create data Object $User->photo = $info [0] ["Savename"]; Save uploaded photos Assemble the $User->add () as needed, and//write user data to the database $this->success ("Data saved successfully!") ”); } |
The first is the instantiation of the upload class
The code is as follows |
Copy Code |
Import ("ORG.Net.UploadFile"); $upload = new UploadFile (); Instantiate the Upload class
|
After instantiating the upload class, you can set up some of the uploaded properties (parameters) and the supported properties are:
MaxSize: The maximum file size (in bytes) for file uploads defaults to-1 size Limited
Savepath: File save path, if left blank will take Upload_path constant defined path
Saverule: Upload file Save rule, must be a function name without any parameters, such as time, uniqid com_create_guid, etc., but must be able to ensure that the generated file name is unique, the default is Uniqid
Hashtype: Hash verification method for uploading files, default is Md5_file
AutoCheck: Automatically detects attachments, defaults to automatic detection
Uploadreplace: Whether a file with the same name is overwritten
Allowexts: Allow uploaded file suffixes (leave blank for unlimited), use array settings, default to empty array
Allowtypes: File types allowed to upload (leave blank for unlimited), using array settings, default to empty array
Thumb: Do you want thumbnail processing of picture files, default to False
Thumbmaxwidth: Maximum width of thumbnails, multiple comma separated
Thumbmaxheight: Maximum height of thumbnails, multiple comma separated
Thumbprefix: File prefix for thumbnails, default to Thumb_ (if you set more than one thumbnail size, please set multiple prefixes here)
Thumbsuffix: Thumbnail file suffix, default is null (if you set more than one thumbnail size, please set multiple suffixes here)
Thumbpath: Thumbnail save path, empty words to upload the directory itself
Thumbfile: Specifies the filename of the thumbnail
Thumbremoveorigin: Whether to delete the original image after the thumbnail is generated
Autosub: Whether to use subdirectories to save uploaded files
Subtype: subdirectory creation method, default is hash, can be set to hash or date
DateFormat: Specifies the date format when the subdirectory is date
Hashlevel: The level of subdirectory preservation, default to one layer
The above properties can be set directly, for example:
The code is as follows |
Copy Code |
$upload->thumb = True $upload->thumbmaxwidth = "50,200″ $upload->thumbmaxheight = "50,200″
|
The ability to generate thumbnails requires the support of the image class.
Set the upload parameters, you can call the UploadFile class upload method for attachment upload, if it fails, return false, and use the Geterrormsg method to get error message; If uploaded successfully, You can get a list of attachment information that was successfully uploaded by calling the Getuploadfileinfo method. So the return value of the Getuploadfileinfo method is an array in which each element is the attachment information that is uploaded. Each attachment information is also an array that records the following information, including:
Key: The form name of the attachment upload
Savepath: Save path to upload file
Name: The original name of the uploaded file
Savename: Save name for upload file
Size: Uploading file sizes
Type: MIME type of upload file
Extension: The suffix type of the uploaded file
Hash: Hash verification string for upload file
After the file is uploaded successfully, you can use the attachment information for other data access operations, such as saving to the current datasheet or a separate attachment datasheet.
If you need to use multiple file uploads, just modify the form to
xml/html Code
The code is as follows |
Copy Code |
<input type= "file" name= "Photo" >
|
To
xml/html Code
The code is as follows |
Copy Code |
<input type= "File" Name= "photo1″> <input type= "File" Name= "photo2″> <input type= "File" Name= "photo3″> Or PHP code <input type= "File" Name= "photo[]" > <input type= "File" Name= "photo[]" > <input type= "File" Name= "photo[]" > |
Two ways of multiple attachment upload System File Upload class can be automatically identified.