Reprinted-ThinkPHP file Upload class sharing _ PHP Tutorial

Source: Internet
Author: User
-Share the ThinkPHP file Upload class. The upload class uses the Net. updateFile class, the latest version of the upload class contains the following functions (some functions need to be combined with other class libraries of ThinkPHP system): 1. basic Upload function 2 Upload class using Net. the UpdateFile class. The latest Upload class contains the following functions (some functions need to be combined with other class libraries in the ThinkPHP system ):

1. basic Upload functions

2. batch upload is supported.

3. support generating image thumbnails

4. Custom parameter Upload

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

6. support definition of the upload type, Attachment size, and upload path

7. support saving and uploading files by hash or date subdirectory

8. security detection for uploaded images

9. supports naming rules for uploaded files

10. support Hash verification of uploaded files


Using the upload function in ThinkPHP requires no special processing. For example, the following is a form submission with attachment Upload:

The code is as follows:

Note that enctype = "multipart/Form-data" must be added to the form label before uploading. Because the form is submitted to the current module's upload operation method, we can add the following upload method in the module class:

The code is as follows:
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 {// the uploaded file is successfully obtained.
$ Info = $ upload-> getUploadFileInfo ();
}
// Save the form data, including the attachment data
$ User = M ("User"); // instantiate the User object
$ User-> create (); // create a data object
$ User-> photo = $ info [0] ["savename"]; // Save the uploaded photo and assemble it as needed
$ User-> add (); // write User data to the database
$ This-> success ("data is saved successfully !");
}

First, instantiate the upload class.

The code is as follows:
Import ("ORG. Net. UploadFile ");
$ Upload = new UploadFile (); // instantiate the upload class

After the upload class is instantiated, you can set some Upload attributes (parameters). the supported attributes include:

MaxSize: the maximum file size (in bytes) uploaded by a file. the default value is-1.

SavePath: specifies the file storage path. if it is left empty, the path defined by the constant UPLOAD_PATH is used.

SaveRule: The storage rule for uploaded files. it must be a function name without any parameters, such as time and uniqid com_create_guid. However, the generated file name must be unique, the default value is uniqid.

HashType: hash verification method for uploading files. the default value is md5_file.

AutoCheck: whether to automatically detect attachments. the default value is automatic check.

UploadReplace: whether the file with the same name is overwritten

AllowExts: specifies the file suffix that is allowed to be uploaded (leave blank is unlimited). It is set using an array. the default value is an empty array.

AllowTypes: specifies the file type that can be uploaded. the default value is an empty array.

Thumb: Specifies whether to perform thumbnail processing on image files. the default value is false.

ThumbMaxWidth: The maximum width of a thumbnail. multiple values are separated by commas (,).

ThumbMaxHeight: the maximum height of a thumbnail. multiple values are separated by commas (,).

ThumbPrefix: the prefix of the thumbnail file. the default value is thumb _ (if you have set multiple thumbnail sizes, set multiple prefixes here)

ThumbSuffix: The file suffix of the thumbnail. it is blank by default. (if you have set multiple thumbnail sizes, set multiple suffixes here)

ThumbPath: The storage path of the thumbnail. if it is left empty, the file upload directory is used.

ThumbFile: specifies the thumbnail file name.

ThumbRemoveOrigin: whether to delete the source image after the thumbnail is generated

AutoSub: whether to use subdirectories to save uploaded files

SubType: Create a subdirectory. the default value is hash. it can be set to hash or date.

DateFormat: specify the date format when the subdirectory mode is date.

HashLevel: the level of sub-directory storage. the default level is one layer.

The preceding attributes can be set directly, for example:

The code is as follows:
$ Upload-> thumb = true
$ Upload-> thumbMaxWidth = "50,200 ″
$ Upload-> thumbMaxHeight = "50,200 ″

The Image class supports generating thumbnails.

After setting the upload parameters, you can call the upload method of the UploadFile class to upload attachments. if the upload fails, false is returned and the getErrorMsg method is used to obtain the error message. if the upload is successful, you can call the getUploadFileInfo method to obtain the list of successfully uploaded attachments. Therefore, the returned value of the getUploadFileInfo method is an array, and each element is the uploaded attachment information. Each attachment is an array that records the following information, including:

Key: The name of the form uploaded by the attachment.

Savepath: save path of the uploaded file

Name: The original name of the uploaded file.

Savename: the name of the uploaded file.

Size: The size of the uploaded file.

Type: MIME type of the uploaded file

Extension: Suffix type of the uploaded file

Hash: hash verification string of the uploaded file

After the file is uploaded successfully, you can use the attachment information to access other data, for example, save it to the current data table or a separate attachment data table.

If you need to upload multiple files, you only need to modify the form

XML/HTML code

The code is as follows:

Change

XML/HTML code

The code is as follows:



Or

PHP code


The file Upload class of the multi-attachment Upload system can be automatically identified.


Merge (some functions need to be combined with other class libraries of the ThinkPHP system): 1. basic Upload 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.