Home  >  Article  >  Backend Development  >  File upload, springmvc file upload_PHP tutorial

File upload, springmvc file upload_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:55:28815browse

File upload, springmvc file upload

File upload

File upload is to upload client files to the server (not a simple copy)

File upload requires two parts: client and server.

Client:

  • Use the file form element in the form to upload files
  • The get method can only transmit textual information, while POST can transmit binary data.
  • enctype: Mainly used to set the method of data transmission
  • Value:
  • aplication/x-www-url-encoded (default) is mainly used to transmit textual data.
  • multipart/form-data Used to set up and transmit multiple form data (text data and binary data)

Server side

  • File upload needs to be enabled in php.ini: file_uploads
  • Temporary directory in php.ini: upload_tmp_dir
  • Control the size of uploaded files in php.ini: upload_maxfile_size

php file upload settings:

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

Encapsulate file upload function

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;

                          

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1116661.htmlTechArticleFile upload, springmvc file upload File upload File upload is to upload client files to the server (not simple copy) File upload requires two parts: client...
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