PHP Image Upload Trilogy

Source: Internet
Author: User

Upload image principle: First of all to determine whether the file type is a picture format, if you upload files, and then rename the file (generally to avoid uploading the file name, and now basically is to be named for the time), and then upload the file to the specified directory, upload the upload image after successful uploading preview.

1. First we start to determine whether the file type is a function of the image type

STRRCHR: Finds the position of the last occurrence of a string in another string and returns all characters from that position to the end of the string.

SUBSTR: Takes a partial string.

$HTTP _post_files[' file ' [' name ']: Gets the full name of the file currently being uploaded.

The picture type is "." The following characters (for example: a filename is called a xxx.jpg then its type is ".") Behind the JPG). We can use the PHP function to intercept the uploader file name.

Let's write a function to get the file type.

<?phpfunction type () {    return substr (STRRCHR ($HTTP _post_files[' file ' [' Name '], '. '), 1);}? >

2. If you upload the file, then rename the function used in the file

Strtolower: Converts all the letters of a string to lowercase letters.

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

Implode: function combines array elements into a single string

Random: Randomly generated numbers

$_files[' UserFile ' [' name ']: Upload file name

$uploaddir: A variable that you define yourself. For example, in the same folder, you want to put the uploaded files in this folder file folder, you can define $uploaddir= "./file/"; Pay attention to the wording.

There will be a lot of problems here, first write an array that can upload the type. Second, judge the legality of the document. Third, the name of the file. * (This way to determine the size of the file is not written) first define an array of types allowed to upload the file:

$type =array ("JPG", "gif", "    bmp", "JPEG", "    png");

Second with a if......else ... Write a control flow statement that determines the legitimacy of a file:


Here's the name of the else{.



$uploadfile = $uploaddir. $name; FileName address after upload}

  

3. Finally upload the file to the specified directory, and then output the function to preview the uploaded image after successful uploading.

Move_uploaded_file: Execute upload file

if (Move_uploaded_file ($_files[' userfile ' [' tmp_name '], $uploadfile)) {    echo ' <center> your file has been uploaded upload picture preview: </center><br><center></center> ';    
} else { echo "Transmission failed! ";
}

  

  

PHP Image Upload Trilogy

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.