PHP Image Upload steps: Detailed Analysis

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff. When learning PHP, you may encounter PHP image uploading problems. Here we will introduce the solutions to the PHP image uploading problem, I will share it with you here. Today we will start to learn how to upload images in PHP. Image uploading principle: first, determine whether the file type is in the Image Format

Welcome to the Linux community forum and interact with 2 million technical staff> when you are learning PHP, you may encounter a PHP Image Upload problem. Here we will introduce how to solve the PHP Image Upload problem, I will share it with you here. Today we will start to learn how to upload images in PHP. Image uploading principle: first, determine whether the file type is in the Image Format

Welcome to the Linux community forum and interact with 2 million technicians>

When learning PHP, you may encounter a PHP Image Upload problem. Here we will introduce the solution to the PHP Image Upload problem. Here we will share it with you. Today we will start to learn how to upload images in PHP. Principles of image uploading: first, determine whether the file type is in the image format. If so, upload the file and rename the file (generally, it is not to upload the file with the same name, but now it is basically named after the time ), next, upload the file to the specified directory, and output the preview of the uploaded image after successful upload.

1. First, we start to determine whether the file type is a function used by the image type.

{

Strrchr: searches for the last occurrence position of a string in another string, and returns all characters from this position to the end of the string.

Substr: obtains some strings.

$ HTTP_POST_FILES ['file'] ['name']: Get the full name of the currently uploaded file.

}

The image type is the character after "." (for example, if a file name is XXX. JPG, the image type is JPG after ). We can use functions in PHP to intercept the name of the uploaded file. Let's write a function to get the file type.

  

Function type ()

{

Return substr (strrchr ($ HTTP_POST_FILES ['file'] ['name'], '.'), 1 );

}

?>

2. If yes, upload the file and rename the function used by the file.

{Strtolower: converts all strings to lowercase letters.

In_array: The function searches for the given value in the array.

Implode: The function combines array elements into a string.

Random: Number of randomly generated values

$ _ FILES ['userfile'] ['name']: name of the uploaded file

$ Uploaddir: a custom variable. For example, if you want to put the uploaded FILE in the same folder, you can define $ uploaddir = "./file/" in this way.

}

There will be many problems here. First, write an array that can be uploaded. Second, determine the validity of the file. Third, rename the file. *

(The file size will not be written here) first define the array of types that are allowed to upload files: $ type = array ("jpg", "gif", "bmp", "jpeg ", "png ");

Second, use an IFelse command to write a control flow statement that determines the validity of a file.

If (! In_arry (strtolower (type (), $ type ))

// If the upload type does not exist

{$ Text = implode ('.', $ type );

Echo "You can only upload the following types of files:", $ text ,"
";

}

The following is to rename them, else

{$ Filename = explode (".", $ _ FILES ['userfile'] ['name']);

// Accurately create an array with the uploaded file name.

$ Time = date ("m-d-H-I-s ");

// Current upload time

$ Filename [0] = $ time;

// Replace t with the file name

Name = implode (".", $ filename );

// Name of the uploaded file

$ Uploadfile = $ uploaddir. $ name;

// Address of the uploaded file name

}

3. Finally, upload the file to the specified directory and output the function {

Move_uploaded_file: Execute the Upload File

}

If (move_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name'], $ uploadfile ))

{

Echo"

You have uploaded the file and uploaded the image to preview it:
";

Echo"

Continue upload ";

}

Else

{

Echo "transmission failed! ";

}

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.