Upload form
The upload form does not require special processing. The following is the simplest single file upload form:
Note: The form must be added with the enctype = "multipart/form-data" attribute to support file upload.
The code is as follows: |
Copy code |
<Form id = "upload" method = 'post' action = "_ URL _/upload/" enctype = "multipart/form-data"> <Input name = "image" type = "file"/> <Input type = "submit" value = "submit"> </Form>
|
Upload operation
The next step is to define the upload operation. We can add the upload operation to the Action controller as follows:
To use the upload function, the first step is to instantiate the upload class:
The code is as follows: |
Copy code |
Import ('org. Net. Uploadfile '); $ Upload = new UploadFile (); // instantiate the upload class // Parameter settings // Set the attachment Upload Directory $ Upload-> savePath = './Uploads /'; // Set the thumbnail to be generated, which is only valid for image files $ Upload-> thumb = true; // Set the file suffix for generating thumbnails $ Upload-> thumbPrefix ='m _, s _ '; // produce two thumbnails. // Set the maximum width of the thumbnail. $ Upload-> thumbMaxWidth = '2017, 50 '; // Set the maximum height of the thumbnail. $ Upload-> thumbMaxHeight = '2017, 50 '; // File upload code Public function upload (){ Import ('org. Net. Uploadfile '); $ Upload = new UploadFile (); // instantiate the upload class $ Upload-> maxSize = 3145728; // sets the attachment upload size. $ Upload-> allowExts = array ('jpg ', 'GIF', 'PNG', 'jpeg '); // you can specify the attachment upload type. $ Upload-> savePath = './Public/Uploads/'; // sets the attachment upload Directory If (! $ Upload-> upload () {// upload error message $ This-> error ($ upload-> getErrorMsg ()); } Else {// Upload successful $ This-> success ('upload successful! '); } } |
Note that only php files are supported. For other types of files, an invalid file is prompted.
The above is a single file upload. To upload multiple files, we only need to simply process the form.
Multi-file Upload
The form is written as follows:
The code is as follows: |
Copy code |
<Input type = 'file' name = 'image1 '> <Input type = 'file' name = 'image2'> <Input type = 'file' name = 'image3 '> Or, for example, the original php multi-file Upload method <Input type = 'file' name = 'image [] '> <Input type = 'file' name = 'image [] '> <Input type = 'file' name = 'image [] '> |
The php processing method is still unchanged for uploading a single file.
The code is as follows: |
Copy code |
Import ("ORG. Net. UploadFile "); $ Upload = new UploadFile (); Foreach ($ _ FILES as $ key => $ file ){ If (! Empty ($ file ['name']) { $ Upload-> autoSub = true; $ Upload-> subType = 'date '; $ Info = $ upload-> uploadOne ($ file ); If ($ info) {// Save the attachment information M ('photo')-> add ($ info ); } Else {// upload error $ This-> error ($ upload-> getErrorMsg ()); } } } |
The upload class of the latest version contains the following functions (some functions need to be combined with other class libraries of the ThinkPHP system ):
Basic Upload function
Batch upload is supported.
Support generating image thumbnails
Custom parameter Upload
Upload detection (including size, suffix, and type)
Supports overwrite Upload
Supports definition of the upload type, Attachment size, and upload path.
Supports saving uploaded files by hash or date subdirectories
Supports dynamic definition of sub-directories to save files
Security detection for uploaded images
Supports naming rules for uploaded files
Supports Hash verification of uploaded files