File Upload and java File Upload

Source: Internet
Author: User

File Upload and java File Upload

There are two steps to upload a file:

1,Client user upload;

A) a form field is required to inject the file to be uploaded,

<input type="file" name="file" />

B) Select the file to be uploaded.

C) the user clicks to send the file to the server

 

2,Server receiving

Form uses the POST submission method.

* Note: GET and POST cannot submit binary files, but you can add an attribute for POST submission.

<form action="file.php" method="POST"  enctype="multipart/form-data">

 

Html section:
Html part: <! DOCTYPE html> 

 

Php Section

Var_dump ($ _ FILES) is an array.

array  'file' =>     array      'name' => string '4.jpg' (length=5)      'type' => string 'image/jpeg' (length=10)      'tmp_name' => string 'F:\wamp\tmp\php904D.tmp' (length=23)      'error' => int 0      'size' => int 273665

Data verification is required.

A) locate the error point based on the two-dimensional array;

 

Switch ($ _ FILES ['file'] ['error']) {case '0': echo "uploaded successfully"; break; case '1': case '2 ': header ('refresh: 3366url%file.html '); echo "file size exceeds server limit"; break; case '3': header ('refresh: 3366url%file.html '); echo "only part of the file is successfully uploaded"; break; case '4': header ('refresh: 3366url1_file.html '); echo "unselected file, please reselect the file and submit "; break; default: echo "File Upload Failed ";}


 

B) after the file is uploaded successfully, it is stored in the specified temporary directory and needs to be changed to the expected file,

Php has a function to complete,Move_uploaded_file ()

Eg:

move_uploaded_file($_FILES['file']['tmp_name'], './cy1/' . $_FILES['file']['name']);

 

Note: tmp_name: the temporary directory on which the file is uploaded to the server,

'Name' indicates the name of the local file on 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.