Processing of uploaded files in php

Source: Internet
Author: User
Let's talk about how to process uploaded files in php. this is a form era...

When we edit our own information in the browser, we will see an upload avatar. in the library, we will upload the document... the word "upload" is everywhere.

Php is the best language (programmers in other languages should not beat me ...). Php has a natural advantage in processing interaction, and naturally has powerful functions to process uploaded files.

Similar to submitting common data, uploading files also requires a form. Create a special form to upload files.

1
 
  
2
  3
  4. uploaded files: 5
  6
  7
  8 9
 

OK. Let's analyze the code snippet.

The preceding enctype specifies the encoding format used when data is sent to the server. It has three values:

MAX_FILE_SIZE the hidden field (in bytes) must be placed before the input field of the file, and its value is the maximum size of the file. This is a suggestion for the browser. php will also check this item. However, this obstacle can be bypassed on the browser side, so do not expect it to block large files. However, the maximum file size is limited by post_max_size = (number) M in php. ini. However, we recommend that you add this project to avoid the trouble of uploading large files only after you have spent time uploading large files.

After the user submits the file form, the server can accept the data. In PHP, the global variable $ _ FILES is used to process FILES. assume that the uploaded field name is userfile (which can be changed randomly in the field ).

$ _ FILES ['userfile'] ['name'] original name of the client file.

$ _ FILES ['userfile'] ['type'] indicates the MIME type of the file. this parameter is not checked on the PHP end, so this value does not necessarily exist.

$ _ FILES ['userfile'] ['size'] size of the uploaded file (in bytes ).

$ _ FILES ['userfile'] ['tmp _ name'] temporary file name stored on the server after the file is uploaded.

$ _ FILES ['userfile'] ['error'] error code related to the file upload. If the upload is successful, the value is 0.

After the file is uploaded, it is stored in the default temporary directory of the server by default. in php. ini, upload_tmp_dir is set to another path.

Here we have to talk about a move_uploaded_file () function:

  

This function checks and ensures thatFileThe specified file is a valid Upload File (that is, it is uploaded through the http post Upload mechanism of PHP ). If the file is valid, move itNewlocThe specified file.

IfFileIf the file is not uploaded legally, no operation is performed. move_uploaded_file () returns false.

IfFileIt is a valid file to be uploaded, but it cannot be moved for some reason and no operation will occur. move_uploaded_file () will return false, and a warning will be issued.

This check is especially important. if the uploaded file may display the content to the user or other users in the system.

The following is an example of php file upload:

1Process uploaded files2
 3
 '; 8 if (move_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name'], $ uploadfile) {9 echo 'file uploaded successfully '.'
'; 10} else {11 echo 'file Upload failed '.'
'; 12} 13 echo' some information about the uploaded File :'.'
'; 14 print_r ($ _ FILES); 15 echo'
'; 16 die (); 17} 18 19?> 20Upload form21
  22
  
   
23
   24
   25 files uploaded: 26
   27
   28
   29
  

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.