This article mainly introduces how to upload multiple PHP files to the server, and explains in detail the array application and data traversal Upload techniques in the multi-file transfer process in the form of examples, it is very practical. if you need it, you can refer to the example below to describe how to upload multiple PHP files to the server. When multiple files are uploaded to the server at the same time, we need to use parameters in the array format to pass and upload data through traversal. the specific operation steps are as follows:
I. 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.
II. 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:
The code 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.
III. 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:
(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:
<? 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 ";
}
?>
IV. skill supplements
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.
I hope this article will help you with PHP programming.