Upload multiple files to the server instance in PHP

Source: Internet
Author: User

Instance description

Uploading images to the server is an essential feature in program development. It not only achieves the purpose of image sharing, but also increases the website access volume and enriches the website content. In this example, we will explain how to use the POST method to upload multiple images.

Key technologies

The key to multifile upload is how to define the name of the uploaded file element and how to determine the number of uploaded files. In this example, the name of the uploaded file is defined as an array (the name of the uploaded file is "files []"). To upload any number of images (within four images), array_filter () and callback functions are used to remove empty elements from the array during file uploading.

The array_filter () function uses the callback function to filter elements in the array. The syntax is as follows:
Array array_filter (array input [, callback])

The array_filter () function transmits each value in the input array to the callback function in sequence. If the callback function returns TRUE, the current value of the input array is included in the returned result array, and the key name of the array remains unchanged.
Note: Do not modify the array in the callback function. For example, add or delete elements in the array. If the array changes, then the function is useless. If the callback () function is not provided, array_filter () will delete the callback function defined in the instance in the input file as check () to verify whether the element value in the array is null. Its syntax is as follows:

The code is as follows: Copy code

Function check ($ var) {// verify whether the returned value of the array is null
Return ($ var! = "");

Note: The POST method is used to upload multiple images. When creating a form, you must specify the enctype = "multipart/form-data" attribute. If you want to control the size of the uploaded file by hiding the value of MAX_FILE_SIZE, the file must be hidden before the file domain to be uploaded. Otherwise, the file will not work.
Design process

(1) create the index. Php file. Add a form, set the file field and submit button, use the POST method, set enctype = "multipart/form-data", submit the data to the index_ OK .php page, and complete the upload of multiple files, the key code is as follows:

The code is as follows: Copy code

<Table width = "750" border = "0" cellspacing = "0" cellpadding = "0">
<Form action = "index_ OK .html" method = "post" enctype = "multipart/form-data" name = "form1">
<Td width = "100" height = "25" align = "right" class = "STYLE1"> Content 1: </td>
<Td width = "150" align = "center"> <input name = "files []" type = "text" id = "files []" size = "15"> </td>
<Td align = "left"> <input name = "picture []" type = "file" id = "pcture []" size> = "30"> </td>
<Td height = "25" align = "right" class = "STYLE1"> content 2: </td>
<Td width = "center"> <input name = "files []" type = "text" id = "files []" size = "15"> </td>
<Td align = "left"> <input name = "picture []" type = "file" id = "pcture []" size> = "30"> </td>
<Td colspan = "3" align = "center">
<Input type = "image" name = "imageField" src = "images/bg_09.jpg"> & nbsp;
<Input type = "image" name = "imgeField2" src = "images/bg_11.jpg"> </td>

(2) in the index. Php file, connect to the database and read the data stored in the database to achieve paging output of the uploaded files. For the code, see related content on the CD.
(3) create an index. Php file to obtain the data submitted in the form, store multiple files to the server, and store the file name and storage path to the database. The code is as follows:

The code is as follows: Copy code

<? Php
Header ("Content-type: text/html; charset = UTF-8"); // sets the file encoding format
Include "conn/conn. php"; // contains the database link file
If ($ _ POST [files]! = ""){
If (! Is_dir ("./upfile ")){
Mkdir ("./upfile"); // Create an upload file storage folder
$ Data = date ("Y-m-d H: m: s"); // defines the time
Function check ($ var) {// verify whether the returned value of the array is null
Return ($ var! = ""); // Returns an array element if it is not null.
$ Files = array_filter ($ _ POST ["files"], "check"); // Remove empty array values
$ Array = array_filter ($ _ FILES ["picture"] ["name"], "check"); // Remove empty array values
Foreach = ($ aarray as $ key => value) {// read the data in the array cyclically
$ Path = 'upfile/'. time (). $ key. strtolower (strstr ($ value, "."); // defines the storage location of the uploaded file
Move_uploaded_file ($ _ FILES ["picture"] ["tmp_name"] [$ key], $ path); // perform the upload operation
$ Query = "insert into tb_up_file (file_test, data, file_name) values ('$ path',' $ data' $ files [$ key] ')";
$ Result = mysql_query ($ query );
Echo "<script>
Alert('upload success: 'Upload success registry.location.href}'index.html '; </script> ";

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.