PHP multiple files are uploaded to the server instance,
This article describes the implementation of multiple PHP files uploaded to the server. For multiple files uploaded to the server at the same time, we need to use the array form of parameter delivery and data traversal upload can be, the specific operation steps are analyzed as follows:
First, the case description
Uploading images to the server is an essential feature of the program development process. It can not only achieve the purpose of image sharing, but also can improve the number of visits to the site, enrich the content of the site. In this example, we explain how to achieve multi-image upload by post.
Second, the key technology
The key to multi-file uploads is how to define the names of the uploaded file elements 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[]"). In order to achieve the purpose of uploading any number of images (within 4 images), the Array_filter () function and the callback function are used to remove empty elements from the array during processing of the uploaded files.
The Array_filter () function, which filters the cells in the array with a callback function, has the following syntax:
Copy the Code code as follows: Array array_filter (array Input[,callback callback])
The Array_filter () function sequentially passes each value in the input array to the callback function. 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 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 input that are equivalent to false.
The callback function defined in this example is check (), which verifies that the element value in the array is empty and has the following syntax:
Copy the Code code as follows: function check ($var) {//Verify that the return value of the array is empty
Return ($var! = "");
}
Note: Multiple picture uploads are implemented via the Post method, and the enctype= "Multipart/form-data" attribute must be specified when creating form forms. If you want to control the size of the uploaded file by hiding the value of the domain max_file_size, you must place the hide before the file domain where the file is uploaded, otherwise it will not work.
Third, the design process
(1) Create a index.php file. Add form, set file field, submit button, use Post method, set enctype= "Multipart/form-data", submit data to index_ok.php page, complete upload operation of multiple files, its key code is as follows:
Copy the Code code as follows:
(2) in the index.php file, connect the database, read the data stored in the database, to achieve the paging output of the uploaded file. Please refer to the relevant content on the CD for the code.
(3) Create a index.php file to get the data submitted in the form, save multiple files to the server, store the file name and storage path to the database, with the following code:
Copy the 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 an 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 empty
Return ($var! = "");//returns the array element if it is not empty
}
$files =array_filter ($_post["Files"], "check");//remove Array hollow value
$array =array_filter ($_files["Picture" ["Name"], "check"); Remove empty array values
Foreach= ($aarray as $key =>value) {//Iterate through the data in the array
$path = ' upfile/'. Time (). $key. Strtolower (Strstr ($value, "."); Define where the upload file is stored
Move_uploaded_file ($_files["picture"] ["tmp_name"] [$key], $path);//Perform upload operation
$query = "INSERT into Tb_up_file (file_test,data,file_name) VALUES (' $path ', ' $data ' ' $files [$key] ')";
$result =mysql_query ($query);
}
echo "";
}
?>
Iv. Skills Supplement
Hides the php file suffix through pseudo-static techniques.
First, modify the Apache server's configuration file httpd.conf. Open the httpd.conf file and navigate to the following location:
Copy the Code code as follows: #LoadModule Rewrite_module modules/mod_rewrite.so
Remove the "#" in front of the item and start the item.
Then, look for the httpd.conf file, locate the allowoverride entry, and modify its value to all. Save and restart the Apache server for the changes to take effect.
Finally, create the. htaccess file under the instance root to implement a hidden operation for the php file suffix. The code for the. htaccess file is as follows:
Copy the Code code as follows: Rewriteengine on# startup item
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]
The file suffix and the passed parameters are matched by regular expressions to complete the hidden operation of the php file suffix.
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/902783.html www.bkjia.com true http://www.bkjia.com/PHPjc/902783.html techarticle php Multiple files uploaded to the server instance, this article describes the PHP multiple files uploaded to the server implementation method. For multiple files uploading to the server at the same time, we need ...