File Upload, springmvc File Upload
File Upload
Uploading a file is to upload the client file to the server (not a simple copy)
Two parts are required for file upload: client and server.
Client:
- Upload a file using the file form element in form
- Only textual information can be transmitted in get mode. binary data can be transmitted in POST mode.
- Enctype: Mainly used to set the data transmission mode.
- Valid value:
- Aplication/x-www-url-encoded (default) is mainly used to transmit text data.
- Multipart/form-data is used to transmit multiple forms (text data and binary data)
Server
- File Upload must be enabled in php. ini: file_uploads
- Temporary directory in php. ini: upload_tmp_dir
- Upload_maxfile_size
PHP File Upload settings:
In php. ini
File_uploads
Example:
Upload_tmp_dir
It is used to set the temporary directory for file upload. The temporary directory features that the temporary file will be deleted immediately after the script is executed.
Upload_max_filesize
Used to set the php limit on the size of uploaded files
Maximum File Uploads per second
Because there is a script in the temporary file directory to delete the file after execution, you need to move the uploaded file to another directory before the script execution ends (dedicated to the directory for storing file uploads)
Move_uploadEd_ File ($ filename, $ target); Function
Note:
$ Filename is the file name.
$ Target is the directory name (User-Defined)
Move $ filename to the directory specified by $ dirname.
$ _ FIELS: used to record information about file uploads.
Encapsulation file upload function
1. define functions
2. Because you need to find the complete file name of the temporary file in the $ _ FILES Array Based on the name attribute value of the form element, you cannot limit the name attribute value of the front-end input form element. The name attribute value of the input form element needs to be dynamically obtained through array_keys in the background.
Example:
Code:
Step 2: Get the temporary file name
Step 2: Get the file suffix
Step 1: encapsulate the function of randomly generated file names
Step 2: Call the file name generation function
Step 2: Move files
File Upload error message
Stored in $ _ FILES ['name attribute name'] ['error']
Example:
Error value:
Upload_err_ OK 0 indicates no error. The upload is successful.
Upload_err _ ini_size 1
Upload_err_form_size 2
Example:
<Input type = "hidden" name = "MAX_FILE_SIZE" value = "2000000"/>
Upload_err_partial 3
Upload_err_no_file 4
Upload_err_no_tmp_dir 6
Upload_err_cant_write 7
Example:
Step 1: Determine the error message
// Determine whether the file is uploaded successfully
Switch ($ arr [$ v] ['error']) {
Case 0:
$ ArrFiles ['filename'] [] = $ fullname;
$ ArrFiles ['code'] [] = 0;
Break;
Case 1:
$ ArrFiles ['filename'] [] = false;
$ ArrFiles ['code'] [] = 'exceeds the PHP file size limited ';
Break;
Case 2:
$ ArrFiles ['filename'] [] = false;
$ ArrFiles ['code'] [] = 'exceeds the size limit of the form file ';
Break;
Case 3:
$ ArrFiles ['filename'] [] = false;
$ ArrFiles ['code'] [] = 'network reasons partially upload ';
Break;
Case 4:
$ ArrFiles ['filename'] [] = false;
$ ArrFiles ['code'] [] = 'no file selected ';
Break;
Case 6:
$ ArrFiles ['filename'] [] = false;
$ ArrFiles ['code'] [] = 'no temporary directory ';
Break;
Case 7:
$ ArrFiles ['filename'] [] = false;
$ ArrFiles ['code'] [] = 'Directory has no write authorization ';
Break;
}