Image upload operations in PHP-php Tutorial

Source: Internet
Author: User
In the frontend programming of the picture Upload operation in PHP, sometimes the viewer must be allowed to upload an image. This function can be implemented through PHP.

1. create a folder for storing images

The folder "file" is created here to store the images uploaded by the user. the folder is empty. you can select the folder creation address, but it is best to create the folder in a convenient location.

2. create an HTML plugin (uploadfile.html)

In TerraForm, we use the form tag of HTML and

  • To select Image files
  • To submit image information

The code is as follows:

   
         

Note:When the action in the form is connected, A. php file that processes image uploads will be created. Optional bytes 2. enctype = "multipart/form-data" must be specified in form attributes ". The enctype attribute specifies how to encode the form data before it is sent to the server. Multipart/form-data is not character encoded. WhenFile upload controlIs required. Upload methods 3. here, we use the post method.

3. create the PHP part (uploadHandler. php) 1. principle

Compile plugin we create a php file, in which only input

 

Then we will print it to a corresponding array arr (1 ). The array structure of   arr (1) is as follows:

Array (1) {["img"] => array (5) {["name"] => string (**) "AAAAAAA.jpg" // image name ["type"] => string (10) "image/jpeg" // image format ["tmp_name"] => string (45) "/Applications/XAMPP/xamppfiles/temp/phpVZQY0m" // image cache address ["error"] => int (0) // Number of uploaded image errors ["size"] => int (124246) // image size }}

Struct has an element ["img"] under the array, while ["img"] is a two-dimensional array, which has five elements: name/type/tmp_name/error/size. Using these attributes, we can get the basic information of an image. Next, we will save the image to a folder by judging these basic attributes.

2. Actual operation

Zookeeper makes the following judgment:

  • Error [with]: error prompt
  • Error [none] in the next step, the following error occurs when encoding type [non-jpeg]: the following error occurs when encoding type [jpeg]: Next Step

If zookeeper satisfies the above judgment, we think this image can be saved. In order to avoid repeated image names, we use the timestamp concept to concatenate a new file name so that each image name is unique.

Then, we can create an array. the elements in the array are in various image formats. compare the image format of the file with the elements in the array. if it is not an accepted image format, it will not be saved.

The code is as follows:

 
4. Upload Images

Upload images at this time, if all items meet the requirements, the uploaded images will be stored in the file folder. Uploaded successfully!

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.