Interpreting the handling of uploaded files in PHP. _ PHP Tutorial

Source: Internet
Author: User
Tags php file upload
Interpreting the handling of uploaded files in PHP ,. Interpreting the handling of uploaded files in PHP, we edit our own information in the browser, and we will encounter uploading portraits. in the library, we will upload documents ...... there is a "upload" everywhere to interpret the handling of uploaded files in PHP,

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.

 

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 that the file specified by the file is a valid file to be uploaded (that is, the file is uploaded through the http post Upload mechanism of PHP ). If the file is valid, it is moved to the file specified by newloc.

If the file is not a valid Upload file, no operation is performed. move_uploaded_file () returns false.

If the file is valid but cannot be moved for some reason, 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:

Process uploaded files<? Phpif (isset ($ _ FILES ['userfile']) {$ uploaddir = 'upload/'; $ uploadfile = $ uploaddir. basename ($ _ FILES ['userfile'] ['name']); echo'
'; If (move_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name'], $ uploadfile) {echo 'file uploaded successfully '.'
';} Else {echo 'file Upload failed '.'
';} Echo' some information about the uploaded File :'.'
'; Print_r ($ _ FILES); echo'
'; Die () ;}?>Upload form
   

Upload, we edit our own information in the browser, and we will upload the avatar. in the library, we will upload the document... there is "upload" everywhere...

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.