PHP image Upload steps: Detailed analysis

Source: Internet
Author: User
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. Image uploading principle: First, determine whether the file type is in the image format. if so, upload the file... "/> <scripttype =" text/javascript "src =" http: // www.

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 letters in the string 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: randomly generated quantity $ _ FILES ['userfile'] ['name']: name of the uploaded file $ uploaddir: custom variables. For example, if you want to put the uploaded FILE in the same folder, you can define $ uploaddir = ". /file/"; note:} there will be many problems. 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"); the second uses an IF .. Else .. Write a control flow statement to determine the validity of a file. If (! In_arry (strtolower (type (), $ type) // if there is no Upload type {$ 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']); // upload the file name to". to create an array for accuracy. $ Time = date ("m-d-H-I-s"); // The current Upload time $ filename [0] = $ time; // replace name t with 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 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.