Chapter 4 upload a file. Learning Tips: 1. PHP Upload configuration 2. $ _ FILES array 3. PHP Upload function although most people think that the Web page only contains webpages, the HTTP protocol can actually transmit any FILES, such as office documents
Learning points:
1. PHP Upload configuration
2. $ _ FILES array
3. PHP Upload function
Although most people think that Web pages only contain webpages, HTTP can actually transmit any file, such as office
Files, PDF files, executable files, AVI files, compressed files, and various other file types. Although FTP has always been
The standard method for uploading files to servers, but uploading files through webpages is also becoming popular.
I. PHP Upload configuration
Some configuration commands can be used to fine-tune the PHP file upload function. These commands are used to determine whether to enable
PHP file upload, maximum file size allowed to be uploaded, maximum allowed script memory allocation, and various other weights
Resource.
1. file_uploads = on | off: determines whether the PHP script on the server can upload files.
2. max_execution_time = integer: the maximum time that a PHP script can run before registering a fatal error.
In seconds.
3. memory_limit = integer: set the maximum memory size that can be allocated by the script, in MB. This prevents
Out-of-control scripts exclusively occupy the server memory.
4. upload_max_filesize = integer: sets the maximum size of the uploaded file, in MB. This command must be small
In post_max_size.
5. upload_tmp_dir = string: Set the temporary location where the uploaded file must be stored on the server before processing,
Until the file is moved to the final destination.
6. post_max_size = integer: determines the maximum size of information acceptable through the POST method, in MB
Unit.
II. $ _ FILES array
HTML of the upload form
ENCTYPE = "multipart/form-data", which is fixed here; otherwise, the file upload fails.
ACTION = "upload. php", which defines the path of the program file to be uploaded
METHOD = "post", which defines the transmission mode as POST. generally, data submitted by Form is set to POST.
<Input type = "hidden" name = "MAX_FILE_SIZE" value = "1000000"> This is a hidden field.
Defines the maximum size of the uploaded file. if this value is exceeded, the upload fails. It must be defined before the file upload domain.
The value defined here cannot exceed the value set in upload_max_filesize in the php. ini file. Otherwise, it does not mean
(Note: the value of MAX_FILE_SIZE is only a suggestion for the browser. In fact, it can be simply
Bypass. Therefore, do not forward browser restrictions to this value. In fact, in the PHP. ini setting
Failed. But it is better to add MAX_FILE_SIZE to the form because it can avoid
It is easy for users to find that the file is too large after they spend time waiting to upload a large file .)
This is the file upload field. the Type attribute must be set to file, but the Name attribute
Can be customized. this value will be used in the code file.
$ _ FILES: a super global variable that stores various upload-related information.
Files uploaded to the server are crucial.
1. the value stored in the $ _ FILES ["userfile"] ["tmp_name"] variable is the temporary storage of the file on the Web server.
Storage location.
2. the value stored in the $ _ FILES ["userfile"] ["n ame"] variable is the file name in the user's system.
3. the value stored in the $ _ FILES ["userfile"] ["s ize"] variable is the file's byte size.
4. the value stored in the $ _ FILES ["userfile"] ["type"] variable is the MIME type of the file, for example, text/plain
Or image/gif.
5. the value stored in the $ _ FILES ["userfile"] ["error"] variable will be any error code related to file upload.
This is a new feature added in PHP4.2.0. Error provides some array constants: 0: indicating no error, 1:
The size of the uploaded file exceeds the specified value. The maximum file size is specified in the PHP configuration file. this command
Is upload_max_filesize. 2: indicates that the size of the uploaded file exceeds the size of the MAX_FILE_SIZE element of the HTML form.
The maximum value. 3: indicates that the file is only partially uploaded. 4: no files are uploaded.
III. PHP Upload function
PHP file system Library provides a large number of file processing functions, in addition, PHP also provides two dedicated
Functions used in the file upload process: is_uploaded_file () and move_uploaded_file ().
1. determine whether to upload the file: is_uploaded_file ()
2. move the uploaded File: move_uploaded_file ()
Note: This article is a video tutorial from Li Yanhui PHP. This article is intended for communication purposes only and cannot be used for commercial purposes. Otherwise, the consequences will be borne by you.
Upload 1.PHP Upload configuration 2. $ _ FILES array 3.PHP Upload function although most people think that the Web only contains webpages, HTTP can actually transmit any FILES, such as office documents...