Home > Article > Backend Development > File upload, springmvc file upload_PHP tutorial
File upload is to upload client files to the server (not a simple copy)
File upload requires two parts: client and server.
There is one in php.ini
file_uploads
Example:
upload_tmp_dir
is used to set the temporary storage directory for file upload. The characteristics of the temporary directory: when the script execution is completed, the temporary file will be deleted immediately.
upload_max_filesize
is used to set the PHP limit on uploaded file size
Limit on the maximum number of files that can be uploaded
Since there will be files in the temporary file directory that will be deleted after the script is executed, the uploaded files need to be moved to another directory (a directory dedicated to storing file uploads) before the script execution ends.
move_uploaded_file($filename,$target); function
Description:
$filename is the file name
$target is the directory name (user-defined)
Move $filename to the directory specified by $dirname
$_FIELS: Mainly used to record information related to file upload
1. Define functions
2. Since the complete file name of the temporary file needs to be found in the $_FILES array based on the name attribute value of the form element, the name attribute value of the front-end input form element cannot be restricted. It is necessary to dynamically obtain the name attribute value of the input form element through array_keys in the background.
Example:
Code:
Step 3: Get the temporary file name
Step 4: Get the file suffix
Step 5: Encapsulate the function of randomly generating file names
Step 6: Call the generate file name function
Step 7: Move Files
File upload error message
is mainly saved in $_FILES['name attribute value']['error']
Example:
error value:
upload_err_ok 0 means there is no error and the upload is successful
upload_err_ ini_size 1
upload_err_form_size 2
Example:
upload_err_partial 3
upload_err_no_file 4
upload_err_no_tmp_dir 6
upload_err_cant_write 7
Example:
Step 8: Judge the error message
//Determine whether the file upload is successful
switch($arr[$v]['error']){
case 0:
break;
case 1:
$arrFiles['filename'][]=false;
break;
case 2:
break;
case 3:
break;
case 4:
$arrFiles['code'][]='No file selected';
break;
case 6:
$arrFiles['code'][]='No temporary directory';
break;
case 7:
break;