To upload multiple files to the server at the same time, we need to use parameters in the form of arrays and upload data through traversal. The specific operation steps are described below. To upload multiple files to the server at the same time, we need to use parameters in the form of arrays and upload data through traversal. The specific operation steps are described below.
Script ec (2); script
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 () deletes all elements in the input that are equal to FALSE.
The callback function defined in this example is check (), which is used to verify whether the element value in the array is null. Its syntax is as follows:
The Code is as follows: |
|
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:
(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: |
|
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 "; } >?
|
Tips
Use pseudo-static technology to hide the suffix of a PHP file.
First, modify the configuration file httpd. conf of the Apache server. Open the httpd. conf file and locate the following location:
The Code is as follows: |
|
# LoadModule rewrite_module modules/mod_rewrite.so
|
Remove "#" before the item and start the item.
Find the httpd. conf file, find the AllowOverride item, and change its value to All. Save and restart the Apache server for the modification to take effect.
Finally, create a. htaccess file under the root directory of the instance to hide the suffix of the PHP file .. The code for the htaccess file is as follows:
The Code is as follows: |
|
RewriteEngine On # startup Item Rewriterulew.index.html $ index. php Rewriteruleappsndex_ OK .html $ index_ OK .php RewriteRule ^ index-([0-9] +)-([0-9] +)-([0-9] +) \. html $ index. php? Vv = $1 & ljjl = $2 & page = $3 [L]
|
Use a regular expression to match the suffix of a file with the passed parameter to hide the suffix of a PHP file.