PHP multiple file Upload to server instance _php tips

Source: Internet
Author: User

This article is an example of how to upload a php multiple files to the server implementation. For multiple files uploaded to the server at the same time, we need to use the array form of parameter transfer and data traversal upload can be, the specific steps of the operation are as follows:

One, the example explanation

Uploading pictures to the server is an essential function in the process of program development. It can not only achieve the purpose of sharing pictures, but also can improve the number of visits to the site, enrich the content of the site. In this example, we explain how to upload multiple images by post.

Second, the key technology

The key to multiple file uploads 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 upload file is defined as an array (the name of the upload file is "files[]"). In order to achieve the goal of uploading any number of images (within 4 images), the Array_filter () function and the callback function are used to remove the empty elements from the array in the process of processing the uploaded files.

Array_filter () function, which filters the cells in the array with the callback function, as follows:

Copy Code code as follows:
Array Array_filter (array Input[,callback callback])

The Array_filter () function passes each value in the input array to the callback function in turn. If the callback function returns True, the current value of the input array is contained in the returned result array, and the array's key name remains unchanged.

Description: Do not modify the array in the callback function, for example, to add or delete elements in the arrays, if the array changes, then the use of this function is meaningless. If the callback () function is not provided, Array_filter () deletes all elements in the input that are equivalent to false.
The callback function defined in this instance is check (), which verifies that the element value in the array is empty and has the following syntax:

Copy Code code as follows:
function Check ($var) {//Verify that the return value of the array is null
Return ($var!= "");
}

Note: The post method to achieve multiple image upload, when creating form forms, you must specify the Enctype= "Multipart/form-data" property. If you want to control the size of the uploaded file by hiding the value of the field max_file_size, you must place the hide before the file field where the file is uploaded, otherwise it will not work.

Third, the design process

(1) Create the index.php file. Add the form, set the File field, submit button, use the Post method, set enctype= "Multipart/form-data", submit the data to the index_ok.php page, complete the upload of multiple files, the key code is as follows:

Copy Code code as follows:
<table width= "750" border= "0" cellspacing= "0" cellpadding= "0" >
<form action= "index_ok.html" method= "post" enctype= "Multipart/form-data" Name= "Form1" >
<tr>
&LT;TD width= "height=" align= "right" class= "STYLE1" > Content 1:</td>
&LT;TD width= "align=" "Center" ><input name= "files[" "type=" text "id=" files[] "size=" ></td>
&LT;TD align= "left" ><input name= "picture[]" type= "file" id= "pcture[" "size>=" ></td>
</tr>
<tr>
&LT;TD height= "align=" "Right" class= "STYLE1" > Content 2:</td>
&LT;TD width= "center" ><input name= "files[]" type= "text" id= "files[" "size=" ></td>
&LT;TD align= "left" ><input name= "picture[]" type= "file" id= "pcture[" "size>=" ></td>
</tr>
<tr>
&LT;TD colspan= "3" align= "Center" >
<input type= "image" Name= "ImageField" src= "images/bg_09.jpg" >
<input type= "image" Name= "ImgeField2" src= "Images/bg_11.jpg" ></td>
</tr>
</form>
</table>

(2) In index.php file, connect the database, read the data stored in the database, realize the paging output of the uploaded file. Please refer to the relevant content in the CD.

(3) Create a index.php file to get the data submitted in the form, store multiple files to the server, and store the file name and storage path in the database with the following code:

Copy Code code as follows:
<?php
Header ("Content-type:text/html;charset=utf-8"); Set file Encoding format
Include "conn/conn.php"; Include database link file
if ($_post[files]!= "") {
if (! Is_dir ("./upfile")) {
MkDir ("./upfile");//create Upload file storage folder
}
$data =date ("y-m-d h:m:s");//define Time
function Check ($var) {//Verify that the return value of the array is null
Return ($var!= "");//Returns an array element if not NULL
}
$files =array_filter ($_post["Files", "check");//remove Array hollow values
$array =array_filter ($_files["picture"] ["name"], "check"); To remove an array of hollow values
Foreach= ($aarray as $key =>value) {//loop read data in array
$path = ' upfile/'. Time (). $key. Strtolower (Strstr ($value, ".")); Define where to store the uploaded files
Move_uploaded_file ($_files["picture"] ["tmp_name"] [$key], $path);//Perform an 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 (' Picture upload success '); window.location.href= ' index.html ';</script> ';
}
?>

Iv. Skills Supplement

Hide the php file suffix through pseudo static technology.
First, modify the Apache server's configuration file httpd.conf. Open the httpd.conf file and navigate to the following location:

Copy Code code as follows:
#LoadModule Rewrite_module modules/mod_rewrite.so

Remove the "#" before the item and start the item.
Then, look for the httpd.conf file, find the allowoverride item in it, and modify its value to all. Save and restart the Apache server for the modifications to take effect.
Finally, create the. htaccess file under the instance root to implement the hide operation on the php file suffix. The code for the. htaccess file is as follows:

Copy Code code as follows:
Rewriteengine on# Boot entry
rewriterule^index.html$ index.php
rewriterule^ndex_ok.html$ index_ok.php
rewriterule^index-([0-9]+)-([0-9]+)-([0-9]+) \.html$ Index.php?vv=$1&ljjl=$2&page=$3[l]

A regular expression is used to match the suffix of the file and the passed parameters to complete the hidden operation of the php file suffix.

I hope this article will help you with your PHP program design.

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.