Php uses the POST method to upload leaflets and multi-files

Source: Internet
Author: User
PHP features-file upload-using the POST method to upload leaflets and multi-file-based features allows users to upload text and binary files. Using PHP authentication and file operation functions, you can fully control which users are allowed to upload files and how to process the files after they are uploaded.

PHP is able to accept any files uploaded from RFC-1867-compliant browsers, including Netscape Navigator 3 and later, with patched Microsoft Internet Explorer 3 or later.

Note: related settings

See file_uploads, upload_max_filesize, upload_tmp_dirpost_max_size, and max_input_time setting options in php. ini.

Example #1 file upload form

You can create a special form to support file upload as follows:

 

In the above example,/index. php/Article should be replaced to point to a real PHP file.

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 received file. This is a suggestion for the browser. PHP will also check this item. This setting can be simply bypassed on the browser side, so do not expect this feature to block large files. In fact, the maximum value of uploaded files in PHP settings will not expire. However, it is best to add this project to the form, because it can avoid the trouble of file Upload failure only after the user spends the time waiting to upload a large file.

Note:

Make sure that the attribute of the file upload form is enctype = "multipart/form-data". Otherwise, the file cannot be uploaded.

The global variable $ _ FILES exists since PHP 4.1.0 ($ HTTP_POST_FILES is used in earlier versions ). This array contains information about all uploaded files.

In the preceding example, the content of the $ _ FILES array is as follows. Assume that the name of the file upload field is userfile, as shown in the preceding example. The name can be named at will.

$ _ FILES ['userfile'] ['name']

The original name of the client machine file.


$ _ FILES ['userfile'] ['type']

The MIME type of the file, if the browser provides this information. An example is "image/gif ". However, this MIME type is not checked on the PHP end, so do not take it for granted.


$ _ 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']

The error code related to the file upload. This project was added in PHP 4.2.0.


After the file is uploaded, it is stored in the default temporary directory of the server by default, unless upload_tmp_dir in php. ini is set to another path. The default temporary directory of the server can be reset by changing the environmental variable TMPDIR of the PHP runtime environment. However, it does not work when you run the putenv () function in the PHP script. This environment variable can also be used to confirm that other operations are also performed on the uploaded file.

Example #2 make the file upload take effect

For more information, see the is_uploaded_file () and move_uploaded_file () functions. The following example processes the file upload provided by the form.

 '; If (move_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name'], $ uploadfile) {echo "File is valid, and was successfully uploaded. \ n ";} else {echo" Possible file upload attack! \ N ";} echo 'here is some more debugging info: '; print_r ($ _ FILES); print"
";?>

Accept the PHP script for uploading files. in order to determine the operations to be performed on the file, you should perform any logical check. For example, you can use the $ _ FILES ['userfile'] ['size'] variable to exclude FILES that are too large or too small, you can also use the $ _ FILES ['userfile'] ['type'] variable to exclude FILES with different file types and certain standards, however, this is only the first step in a series of checks, because this value is completely controlled by the client and not checked on the PHP side. Starting from PHP 4.2.0, you can use the $ _ FILES ['userfile'] ['error'] variable to plan the next step based on different error codes. In any case, you can either delete the file from the temporary directory or move it to another place.

If no uploaded file is selected in the form, the value of the PHP variable $ _ FILES ['userfile'] ['size'] is 0, $ _ FILES ['userfile'] ['tmp _ name'] will be empty.

If the file is not moved to other places or renamed, the file will be deleted at the end of the form request.

Example #3 upload a group of files

PHP's HTML array feature even supports file types.

 
  $error) {    if ($error == UPLOAD_ERR_OK) {        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];        $name = $_FILES["pictures"]["name"][$key];        move_uploaded_file($tmp_name, "data/$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.