Home  >  Article  >  Backend Development  >  Analysis of form upload examples in PHP

Analysis of form upload examples in PHP

小云云
小云云Original
2018-03-31 09:29:341923browse


This article mainly shares with you the analysis of form upload examples in PHP, mainly in the form of text and code, hoping to help everyone.
Form upload: data from browser to server. (Transmitted when the browser sends a request to the server.)

The data in the form is divided into two types: 1. String type (byte stream encoding, that is, there are certain encoding rules)

2. File type (binary encoding, that is, only binary transmission, not followed the code rules)

Rrree

Form tag Action, i.e. table specifies a server script to handle the uploaded form. If the action attribute is omitted, it indicates the current script processing;

The method attribute specifies the http method used when submitting the form. The default is GET, and POST and GET are optional. (The form data is visible in the address bar when using GET, but not visible in POST);

The enctype attribute specifies how the form data should be encoded before being sent to the server. The attribute value is application/x-www-form-urlencode (This is the default option, which means all strings are encoded), multipart/form-data (does not encode it, this value must be used when uploading files), text/plain (spaces are converted to +, but special characters are not encoded) Encoding)

In the input tag:

The type attribute can include text (text input), radio (radio button), checkbox (multiple selection), submit (form submission), password (password) : Use encrypted form), file (file upload).


The name attribute is required. After submitting the form, the value of each control is stored in the $_GET or $_POST array, and name is used as the array subscript of the corresponding value.


The value attribute represents the value of the space. If there is external input, the value of the external input is stored, which is equivalent to a default value. This attribute is not required.


Data reception

After receiving the data from the browser, the server will use different processing methods according to the data type:


1. String type data is stored in $_GET/$_POST variables.


              2. File data is stored in a temporary directory (saved in a script cycle class, and will be automatically deleted when expired)

<form action=&#39;&#39; method=&#39;&#39; enctype=&#39;&#39;>
        <input type=&#39;&#39; name=&#39;&#39; value=&#39;&#39;>
</form>


The password is not displayed here because it is a type='password' attribute.

After the upload is successful, var_dump($_POST) under the script specified in the server background will get the following results:


Only the filled-in string in the result type data, and the index value is the form name, but the file data does not exist (it is stored in the system's temporary file temp, and its existence time is the script cycle).

The temporary directory for uploaded files can be configured in php.ini (closed by default). If it is not changed manually, the default temporary file directory temp of the server system is used.

Data processing

Mainly stores uploaded files (temporary files are automatically deleted after the script ends).

Use function: move_uploaded_file (temporary storage address, target address);

The $_FILES array stores the information of the uploaded file.



This 'FILENAME' is bidding is the name property written in the form.

The name in the two-dimensional array represents the original file name, type represents the file type (not the suffix), tmp_name represents the temporary storage address, error represents whether there is an error and the error type, and size represents the size of the uploaded file.

Series: Strrchr (string, character); get the position of the last time the character, and output the character and all the characters later.

# Strchr (string, character); get the location of the first character. . . . . . . . . . . . . . .

Pathinfo (variable); Get the address, type, and name of the variable. . .

                                                                                                                                                                                                                                    uniqID (‘li_’) means adding the li_ prefix before a generated string.

Related recommendations:

How to process form upload files in php

The above is the detailed content of Analysis of form upload examples in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn