Three steps for uploading images using PHP _ PHP Tutorial

Source: Internet
Author: User
PHP uploads images in three steps for detailed analysis. 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, when we start learning PHP together, you may encounter a PHP image upload problem. here we will introduce the solution to the PHP image upload problem and 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.

 
 
  1. {
  2. 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.
  3. Substr: obtains some strings.
  4. $ HTTP_POST_FILES ['file'] ['name']: Get the full name of the currently uploaded file.
  5. }

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.

 
 
  1. function type()
  2. {
  3. return substr(strrchr($HTTP_POST_FILES['file']['name'],'.'),1);
  4. }
  5. ?>

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

 
 
  1. {
  2. Strtolower: converts all letters in a string to lowercase letters.
  3. In_array: The function searches for the given value in the array.
  4. Implode: The function combines array elements into a string.
  5. Random: number of randomly generated values
  6. $ _ FILES ['userfile'] ['name']: name of the uploaded file
  7. $ Uploaddir: a custom variable. For example, in the same folder, if you want to put the uploaded FILE in the FILE folder of this folder, you can define $Uploaddir="./File /"Note the writing method
  8. }

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.

  
  
  1. If (! In_arry (strtolower (type (), $ type) // if no type can be uploaded
  2. {
  3. $Text=Implode('.', $ Type );
  4. Echo "you can only upload the following types of files:", $ text ,"<Br>";
  5. }

The following is to rename them,

  
  
  1. Else
  2. {
  3. $Filename=Explode(".", $ _ FILES ['userfile'] ['name']); // you can accurately create an array for the uploaded file name.
  4. $Time=Date("M-d-H-I-s"); // The current Upload time.
  5. $ Filename [0] = $ time; // replace the file name t
  6. Name=Implode(".", $ Filename); // name of the uploaded file
  7. $Uploadfile= $ Uploaddir. $ name; // address of the uploaded file name
  8. }

3. finally, upload the file to the specified directory, and output the function used to preview the uploaded image after successful upload.

  
  
  1. {
  2. Move_uploaded_file: execute the Upload file
  3. }
  4. If (move_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name'], $ uploadfile ))
  5. {
  6. Echo"<Center>You have uploaded the file and uploaded the image to preview it: Center><Br><Center><Img Src='$ Uploadfile'> Center>";
  7. Echo"<Br><Center><A Href='Corruption Cr partition pt: history. go (-1 )'>Continue Upload A> Center>";
  8. }
  9. Else
  10. {
  11. Echo "transmission failed! ";
  12. }

In this way, you can write a simple program for uploading images. If you want to improve your functions, you can add them on your own!


Bytes. Today we will start learning together...

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.