File upload-nannong trail

Source: Internet
Author: User
Tags php file upload
File upload-nannong gossip 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:

 

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;

}

Related Article

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.