Multiple PHP files are uploaded to the server instance,

Source: Internet
Author: User

Multiple PHP files are uploaded to the server instance,

This example describes 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:
Copy codeThe 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:

Copy codeCode: 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:

Copy codeThe Code is 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>
<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>
</Tr>
<Tr>
<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>
</Tr>
<Tr>
<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 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:

Copy codeThe 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:

Copy codeThe 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:

Copy codeThe 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.




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.