Home > Article > Backend Development > Analysis of form upload examples in PHP
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)
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
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='' method='' enctype=''> <input type='' name='' value=''> </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
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!