File upload, springmvc file Upload _ PHP Tutorial

Source: Internet
Author: User
Tags php file upload
File upload: springmvc file upload. File upload: springmvc file upload File upload is to upload the client files to the server (not a simple copy) file Upload requires two parts: client 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.

  • 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)

  • 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




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.


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


$ 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.



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']


Error value:

Upload_err_ OK 0 indicates no error. the upload is successful.

Upload_err _ ini_size 1

Upload_err_form_size 2



Upload_err_partial 3

Upload_err_no_file 4

Upload_err_no_tmp_dir 6

Upload_err_cant_write 7


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;


Case 1:

$ ArrFiles ['filename'] [] = false;

$ ArrFiles ['code'] [] = 'exceeds the php file size limited ';


Case 2:

$ ArrFiles ['filename'] [] = false;

$ ArrFiles ['code'] [] = 'exceeds the size limit of the form file ';


Case 3:

$ ArrFiles ['filename'] [] = false;

$ ArrFiles ['code'] [] = 'network reasons partially upload ';


Case 4:

$ ArrFiles ['filename'] [] = false;

$ ArrFiles ['code'] [] = 'No file Selected ';


Case 6:

$ ArrFiles ['filename'] [] = false;

$ ArrFiles ['code'] [] = 'no temporary directory ';


Case 7:

$ ArrFiles ['filename'] [] = false;

$ ArrFiles ['code'] [] = 'directory has no write authorization ';



Upload a file upload is to upload a client file to the server (not a simple copy) file Upload requires two parts: client...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.