Interpreting the handling of uploaded files in php, _php tutorial

Source: Internet
Author: User
Tags http post

Explain the problem of uploading files in PHP,


We edit our own information in the browser, we will encounter the upload picture, in the library, we will upload the document ... The word "upload" exists everywhere.

PHP is the best language (Other language program apes don't hit me ... )。 PHP has a natural advantage in dealing with interactions, and naturally has powerful functions to handle uploading files.

Like submitting general data, uploading a file also requires a form. Create a special form below to upload the file.

OK, let's analyze This code snippet.

The enctype above specifies what encoding format the data is used for when it is sent to the server. It has a value of three:

Max_file_size hidden Fields (in bytes) must precede the file input field with a value that is the maximum size of the file. This is a suggestion for the browser, PHP will also check this. However, you can bypass this barrier on the browser side, so don't expect to use it to block large files. However, the maximum file size is limited by the post_max_size= (number) m in php.ini . But the best thing to do is add the project, which avoids the hassle of having to wait for a large file to be uploaded before the user discovers a large file upload failure.

After the user submits the file form, the server side can accept the data. PHP has a global variable $_files to process the file, assuming that the upload field name is UserFile (can be changed in the field arbitrarily).

$_files[' UserFile ' [' Name '] The original name of the client file.
The MIME type of the $_files[' userfile ' [' type '] file is not checked on the PHP side, so this value is not necessarily available.
$_files[' userfile ' [' size '] the size (in bytes) of the uploaded file.
$_files[' UserFile ' [' tmp_name '] files are uploaded on the server side after the temporary file name is stored.
$_files[' userfile ' [' Error '] and the file upload related error code. If the upload is successful, the value is 0.

After the file is uploaded, it is stored to the server default temp directory by default, and the Upload_tmp_dir in PHP.ini is set to a different path.

Here you have to say a move_uploaded_file () function:

This function checks and ensures that files specified by file are valid upload files (i.e. uploaded via PHP's HTTP POST upload mechanism). If the file is valid, move it to the file specified by Newloc.

If file is not legitimately uploaded, no action will occur and Move_uploaded_file () will return false.

If file is a legitimate upload, but for some reason it cannot be moved, nothing happens, Move_uploaded_file () returns false, and a warning is issued.

This check is especially important if the uploaded file is likely to cause the user or other users of the system to display its content.

Here is an example of a PHP upload file:

Upload file processing<?phpif (Isset ($_files[' UserFile ')) {$uploaddir = ' upload/'; $uploadfile = $uploaddir. BaseName ($_files[' userfile ') [' name ']); Echo '
 ';} else {echo ' failed to upload file '. ' 
';} Echo ' This is some information about uploading a file: '. '
';p rint_r ($_files), echo '
 ';d ie ();}?  Upload Form  The enctype genus in the!--form must be consistent with the following definition--> 

< span id= "url" itemprop= "url" >http://www.bkjia.com/phpjc/1133052.html www.bkjia.com true http://www.bkjia.com/phpjc/1133052.html techarticle read the handling of uploading files in PHP, We edit our own information in the browser, we will encounter the upload picture, in the library, we will upload the document ... There are "uploads" everywhere ...

  • Related Article

    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.