Php multi-File Upload implementation principle and code

Source: Internet
Author: User

Today, I made a little research on the Multi-image upload function and sorted out the following code to facilitate future use and communication.
1、upload.html page, that is, the file with input type = file:
Copy codeThe Code is as follows:
<Html>
<Head>
<Title> </title>
</Head>
<Body>
<Form enctype = "multipart/form-data" action = "do_upload.php" method = "POST">
<! -- Enctype = "multipart/form-data" in the form indicates to set the MIME encoding of the form. By default, the encoding format is application/x-www-form-urlencoded and cannot be used for file upload. Only multipart/form-data can be used to completely transmit file data, perform the following operations. -->
<Fieldset>
<Legend> Image Upload </legend>
The first image <input name = "userfile" type = "file"> <br>
Figure 2 <input name = "userfile" type = "file"> <br>
Figure 3 <input name = "userfile" type = "file"> <br>
Figure 4 <input name = "userfile" type = "file"> <br>
Figure 5 <input name = "userfile" type = "file"> <br>
<! -- Transfer data in the form of an array -->
<Input type = "submit" value = "Send File">
</Fieldset>
</Form>
</Body>
</Html>

2. do_upload.php page, that is, the page for processing Multifile uploads:
Copy codeThe Code is as follows:
<? Php
// Global Array $ _ FILES
// $ _ FILES ['userfile'] ['tmp _ name'] location where the file is temporarily stored on the web Server
// $ _ FILES ['userfile'] ['name'] file name in the user's system
// $ _ FILES ['userfile'] ['SIZE'] File size in bytes
// $ _ FILES ['userfile'] ['type'] MIME type, text/plain, image/gif
// $ _ FILES ['userfile'] ['error'] error Code related to File Upload
?>
<? Php
// Use the for loop to obtain the transmitted data, which is a three-dimensional data.
For ($ I = 0; $ I <count ($ _ FILES ['userfile'] ['tmp _ name']); $ I ++)
{
$ Upfile = $ new_folder. "/". $ _ FILES ['userfile'] ['name'] [$ I]; // you can modify it as needed.
If (move_uploaded_file ($ _ FILES ['userfile'] ['tmp _ name'] [$ I], $ upfile )){
Echo "no.". ($ I + 1). "The image has been uploaded successfully. <br> ";
}
Else {
Echo "no." ($ I + 1). "Images cannot be uploaded <br> ";
}
}
?>

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.