Reprint-thinkphp File Upload class sharing _php tutorial

Source: Internet
Author: User
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 other class libraries of the thinkphp system):

1. Basically transfer function

2. Support Batch Upload

3. Support to generate image thumbnails

4. Custom parameter Uploads

5. Upload detection (including size, suffix, and type)

6. Support upload type, attachment size, upload path definition

7. Support Hash or date sub-directory save upload file

8. Security detection of uploaded images

9. Support for uploading file naming rules

10. Support for hash verification of uploaded files


No special handling is required to use the upload feature in thinkphp. For example, here is a form submission with an attachment upload:

The code is as follows Copy Code

Note that you must add Enctype= "Multipart/form-data" files in the form label of the forms to upload them. Because the form is submitted to the upload action method of the current module, we add the following upload method to the module class:

The code is as follows Copy Code
Public function upload () {
Import ("ORG.Net.UploadFile");
$upload = new UploadFile (); Instantiating an upload class
$upload->maxsize = 3145728; Set attachment upload Size
$upload->allowexts = array (' jpg ', ' gif ', ' PNG ', ' jpeg '); Set Attachment upload Type
$upload->savepath = './public/uploads/'; Set Attachments Upload Directory
if (! $upload->upload ()) {//Upload error error message
$this->error ($upload->geterrormsg ());
}else{//upload successfully get upload file information
$info = $upload->getuploadfileinfo ();
}
Save form data including attachment data
$User = M ("User"); Instantiating a User object
$User->create (); Creating Data Objects
$User->photo = $info [0]["Savename"]; Save uploaded photos assemble yourself as needed
$User->add (); Write user data to 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 (); Instantiating an upload class

After instantiating the upload class, you can set up some uploaded properties (parameters) and the supported properties are:

MaxSize: The maximum file size (in bytes) for file uploads defaults to-1 unlimited size

Savepath: File save path, if left blank will take path defined by Upload_path constant

Saverule: The upload file save rule, must be a function name without any parameters, such as can be time, uniqid com_create_guid, etc., but must be able to ensure that the generated file name is unique, the default is Uniqid

Hashtype: The hash verification method for uploading files, default is Md5_file

AutoCheck: Whether attachments are automatically detected and automatically detected by default

Uploadreplace: Exists if the file with the same name is overwrite

Allowexts: Allow upload of file suffix (leave blank for unlimited), use array settings, default to empty array

Allowtypes: Allow upload of file types (leave blank for unlimited), use array settings, default to an empty array

Thumb: If you need to do thumbnail processing of the picture file, the default is False

Thumbmaxwidth: The maximum width of the thumbnail, with multiple commas

Thumbmaxheight: Maximum height of thumbnails, multiple separated by commas

Thumbprefix: File prefix for thumbnails, default to Thumb_ (if you set multiple thumbnail sizes, set multiple prefixes here)

Thumbsuffix: Thumbnail file suffix, default is empty (if you set multiple thumbnail size, please set multiple suffixes here)

Thumbpath: Thumbnail save path, left blank to take the file upload directory itself

Thumbfile: Specifies the filename of the thumbnail image

Thumbremoveorigin: Whether to delete the original image after generating the thumbnail

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 mode is date

Hashlevel: The hierarchy of subdirectories saved, 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.

After setting the uploaded parameters, you can call the upload method of the UploadFile class to upload the attachment, if it fails, return false, and use the Geterrormsg method to get the error message, if the upload is successful, You can get a list of successfully uploaded attachment information by calling the Getuploadfileinfo method. So the return value of the Getuploadfileinfo method is an array in which each element is the uploaded attachment information. Each attachment information is another array that records the following information, including:

Key: Attachment Upload Form name

Savepath: Save path for uploaded files

Name: The original name of the uploaded file

Savename: Save name of uploaded file

Size: Sizes of uploaded files

Type: The MIME type of the uploaded file

Extension: The suffix type of the uploaded file

Hash: The hashed validation string of the uploaded file

Once the file has been uploaded successfully, other data access operations such as saving to the current data table or a separate attachment data table can be done through the attachment information.

If you need to use multiple file uploads, just modify the form to

xml/html Code

The code is as follows Copy Code

Switch

xml/html Code

The code is as follows Copy Code



Or

PHP code


The file upload class of the multi-attachment uploading system can be automatically recognized in two ways.


http://www.bkjia.com/PHPjc/444697.html www.bkjia.com true http://www.bkjia.com/PHPjc/444697.html techarticle 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 other class libraries of the thinkphp system): 1. Basically the function 2 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.