Php file upload class. upload. php Usage example

Source: Internet
Author: User
Tags file size file upload php file php and php file upload

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 ".

The code is as follows: Copy code

<Form enctype = "multipart/form-data" method = "post" action = "upload. php">

<Input type = "file" size = "32" name = "image_field" value = "">

<Input type = "submit" name = "Submit" value = "upload">

</Form>

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: Copy code

$ 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: Copy code

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: Copy code

$ 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: Copy code

$ 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: Copy code

$ 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: Copy code

$ Handle-> file_new_name_ext = 'txt'

File extension

The code is as follows: Copy code

$ Handle-> mime_check = true;

Hosts File

The code is as follows: Copy code

$ Handle-> allowed = array ('application/pdf ', 'application/msword', 'image /*');

Specifies the mime type of the file to be uploaded

The code is as follows: Copy code

$ 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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.