Php post method for uploading files

Source: Internet
Author: User

When learning PHP, you may encounter a PHP Upload problem. Here we will introduce the PHP Upload solution and share it with you. This feature allows users to upload text and binary files. Using PHP authentication and file operation functions, you can fully control who are allowed to upload files and how to process PHP files uploaded after the files are uploaded. Note that PHP also supports file upload using the PUT method. This method is used by Netscape Composer and W3C Amaya clients.

Example 1. File Upload form

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

 
  1. <!-- The data encoding type, enctype, MUST be specified as below --> 
  2. <form enctype="multipart/form-data" action="__URL__" method="POST"> 
  3. <!-- MAX_FILE_SIZE must precede the file input field --> 
  4. <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> 
  5. <!-- Name of input element determines name in --> 

Php post upload Method

This feature 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 that PHP also supports file upload using the PUT method. This method is used by Netscape Composer and W3C Amaya clients.

Example 2. File Upload form

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

 
  1. ___FCKpd___0 

In the above example, _ URL _ 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.

 
  1. $_FILES['userfile']['name'] 

The original name of the client machine file.

 
  1. $_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.

 
  1. $_FILES['userfile']['size'] 

Size of the uploaded file, in bytes.

 
  1. $_FILES['userfile']['tmp_name'] 

Temporary File Name stored on the server after the file is uploaded.

 
  1. $_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.


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.