Main steps for uploading images using PHP _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
The main steps for uploading images in PHP are analyzed. When we upload images in practice: First, determine whether the file type is in the image format. if so, upload the file and rename the file. (generally, you do not need to upload the file with the same name.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.

PHP uploads images. 1. First, we start to determine whether the file type is an image type function.

{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) ;}?>

PHP uploads an image. 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/" 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"); 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,

 
 
  1. Else {
  2. $ Filename = explode (".", $ _ FILES ['userfile'] ['name']);
  3. // Set the uploaded file name to "." as an array.
  4. $ Time = date ("m-d-H-I-s ");
  5. // The current Upload time $ filename [0] = $ time;
  6. // Replace t with the file name
  7. Name = implode (".", $ filename );
  8. // Name of the uploaded file
  9. $ Uploadfile = $ uploaddir. $ name;
  10. // Address of the uploaded file name}

PHP uploads an image. 3. upload the file to the specified directory. after the file is uploaded successfully, the function used to preview the uploaded image is output.

 
 
  1. {Move_uploaded_file: upload the file}
  2. If (move_uploaded_file
  3. ($ _ FILES ['userfile'] ['tmp _ name'], $ uploadfile ))
  4. {Echo"
  5. You have uploaded the file and uploaded the image to preview it:
  6. Center>
  7. Center> ";
  8. Echo"
  9. Continue Upload a> center> ";}
  10. Else {echo "transmission failed! ";}

The above is an introduction to PHP image uploading methods, and I hope to help you.


Upload: First, determine whether the file type is in the image format. if so, upload the file and rename the file. (generally, you do not need to upload the file with the same name...

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.