This article introduces how to use the php File Upload class. We hope this instance will be of great help to php beginners. This article introduces how to use the php File Upload class. We hope this instance will be of great help to php beginners.
Script ec (2); script
Introduction
Class. upload. php is a php File upload Class used to manage uploaded files. It helps you quickly integrate the file upload function for your website. In addition, this category also provides some column processing functions to process uploaded files or local files. The image processing function is powerful, the options include scaling, rotating, cropping, type conversion, applying filters, and adding border text and watermarks. Supported image types include png, jpg, gif, and bmp.
Usage
First, we need to have a form for submitting the upload file, as shown below. Note that the form should be written with enctype = "multipart/form-data ".
Create the form processing script file upload. php and add the following program to the script. For more information about the meaning, see the notes.
The Code is as follows: |
|
$ Handle = new upload ($ _ FILES ['image _ field']); |
// Image_field is the name attribute of the upload control in the form. Create an instance of the class and initialize it with $ _ FILES ['image_field ']. Now the class knows which file you want to process, and knows the file location, size, and other information.
The Code is as follows: |
|
If ($ handle-> uploaded ){ // This judgment is a security option. Check if this file is actually uploaded through a regular channel. $ Handle-> file_new_name_body = 'image _ resized '; // name the file. Note that this is part without an extension. If there is a file with the same name, the default behavior is rename. $ Handle-> image_resize = true; // you need to scale an image and set the zoom attribute to true. $ Handle-> image_x = 100; // the scale must be standard. Here we use the width as the reference, and the width must be 100 pixels. $ Handle-> image_ratio_y = true; // you can specify a specific length value in the format of $ handle-> image_y = [value]. here, the program is told to scale proportionally based on the width. $ Handle-> process ('/home/user/files/'); // you can set more processing options before executing this sentence, for example, if you want to rotate and crop the image, you can use this sentence to process and upload the image. During image processing, a copy of the original image will be created without modifying the original file. The original file is stored in a temporary php folder and may be the/tmp directory on the linux server. In this way, you can process the same uploaded image multiple times and specify the uploaded image to different locations. Here '/home/user/files/' specifies the location where the file will be copied. If ($ handle-> processed ){ Echo 'image resized '; $ Handle-> clean (); // if the file is successfully uploaded, the reference to the source file is cleared, after that, you will no longer be able to process and copy images uploaded to temporary folders. } Else { Echo 'error: '. $ handle-> error; } } |
It is useful to download images directly if you are an online image processing program.
The Code is as follows: |
|
$ Handle = new upload ($ _ FILES ['image _ field']); Header ('content-type: '. $ handle-> file_src_mime ); Header ("Content-Disposition: attachment; filename =". rawurlencode ($ handle-> file_src_name ).";"); Echo $ handle-> Process (); |
How to output images directly to the browser
The Code is as follows: |
|
$ Handle = new upload ($ _ FILES ['image _ field']); Header ('content-type: '. $ handle-> file_src_mime ); Echo $ handle-> Process (); |
Commonly used processing parameters, which can be set before calling $ handle-> process ()
The Code is as follows: |
|
$ Handle-> file_new_name_body = 'new name' |
The name of the specified file after it is uploaded to the specified location, excluding the extension.
The Code is as follows: |
|
$ Handle-> file_new_name_ext = 'txt' |
File Extension
The Code is as follows: |
|
$ Handle-> mime_check = true; |
Hosts file
The Code is as follows: |
|
$ Handle-> allowed = array ('application/pdf ', 'application/msword', 'image /*'); |
Specifies the mime type of the file to be uploaded
The Code is as follows: |
|
$ Handle-> image_max_width = 200; |
Specify the maximum allowed image width. If the image width is exceeded, the image cannot be uploaded because it does not meet the requirements. For example, $ handle-> image_max_height, $ handle-> image_max_pixels, $ handle-> image_max_ratio
File Information that you can read before processing a file
File_src_name: the original name of the uploaded file, including the extension.
File_src_name_body: the original name, excluding the extension.
File_src_name_ext: Extension
File_src_pathname: complete file path and name
File_src_mime: mime type
File_src_size: File Size
File_src_error: Upload Error
File_is_image: boolean type, whether it is an image
If the file is an image, you can also read
Image_src_x, image_src_y, image_src_pixels, image_src_type, image_src_bits
It is also useful if you want to record the file information to the database or display it to the user.
File_dst_path: Path of the uploaded file
File_dst_name_body: name of the uploaded file, excluding the extension.
File_dst_name_ext: file extension after upload
File_dst_name: full name of the uploaded file
File_dst_pathname: complete path and name of the uploaded file
If the file is an image, you can also read
Image_dst_x, image_dst_y, image_convert