File upload, springmvc file upload _php tutorial

Source: Internet
Author: User
Tags php file upload

File upload, Springmvc file upload


File Upload

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

File upload requires two parts: client, server side.

Client:

    • File upload using form elements in form
    • For the Get method to pass only textual information, post can pass binary data
    • Enctype: Primarily used to set the way data is passed
    • Value:
    • Aplication/x-www-url-encoded (default) is primarily used to transfer textual data.
    • Multipart/form-data for setting up multiple form data (text data and binary data)

Server-side

    • PHP.ini need to open File upload: file_uploads
    • Temporary directory in php.ini: Upload_tmp_dir
    • PHP.ini in control upload file large: upload_maxfile_size

PHP File Upload settings:

There is a php.ini in the

File_uploads

Example:

Upload_tmp_dir

Used to set the temporary saved directory of File upload, temporary directory features: When the script execution is finished, the temporary file will be deleted immediately.

Upload_max_filesize

Used to set PHP limits on the size of uploaded files

A limit on the maximum number of files that can be uploaded

Because there will be script execution end delete files in the temp file directory, you will need to move the uploaded files before the end of the script execution (the directory dedicated to storing file uploads)

Move_uploadEd_file ($filename, $target); function

Description

$filename is the file name

$target is the directory name (user-defined)

Move $filename to the specified directory $dirname

$_fiels: Mainly used to record the file upload information related

Encapsulate File Upload function

1. Defining functions

2. Because the Name property value of the form element is required, the full filename of the temporary file is found in the $_files array, and the name attribute value of the input form element in the front end cannot be restricted to death. The Name property value of the input form element needs to be retrieved dynamically through Array_keys in the background

Example:

Code:

3rd step: Get temporary file names

4th step: Get the file suffix

5th step: Package Randomly generated file name function

6th step: Call the Generate file name function

7th step: Moving Files

File Upload error message

Mostly saved in $_files[' name attribute value ' [' Error ']

Example:

Value of error:

UPLOAD_ERR_OK 0 means no error, upload succeeded

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:

8th step: Judging the error message

Determine if the file upload was successful

switch ($arr [$v] [' ERROR ']) {

Case 0:

$arrFiles [' filename '][]= $fullname;

$arrFiles [' Code '][]=0;

Break

Case 1:

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

$arrFiles [' Code '][]= ' exceeds php file size limit ';

Break

Case 2:

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

$arrFiles [' Code '][]= ' exceeds the form form file size limit ';

Break

Case 3:

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

$arrFiles [' Code '][]= ' network cause partial upload ';

Break

Case 4:

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

$arrFiles [' Code '][]= ' no file selected ';

Break

Case 6:

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

$arrFiles [' Code '][]= ' no temp directory ';

Break

Case 7:

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

$arrFiles [' Code '][]= ' directory does not have write permission ';

Break

}

http://www.bkjia.com/PHPjc/1116661.html www.bkjia.com true http://www.bkjia.com/PHPjc/1116661.html techarticle File upload, springmvc file Upload file Upload file Upload is the client's files uploaded to the server side (not a simple copy) file upload requires two parts: the 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: info-contact@alibabacloud.com 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.