Home > Backend Development > PHP Tutorial > PHP upload multiple files to server instance, _PHP tutorial

PHP upload multiple files to server instance, _PHP tutorial

WBOY
Release: 2016-07-13 10:15:46
Original
743 people have browsed it

PHP multiple files upload to server instance,

The example in this article describes the implementation method of uploading multiple files to the server in PHP. For the situation where multiple files are uploaded to the server at the same time, we need to use parameter transfer in the form of arrays and traversal upload of data. The specific operation steps are analyzed as follows:

1. Example

Uploading pictures to the server is an essential function in the program development process. It can not only achieve the purpose of image sharing, but also increase the traffic of the website and enrich the content of the website. In this example, we explain how to upload multiple images through POST.

2. Key technologies

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, define the name of the uploaded file in the form of an array (the name of the uploaded file is "files[]"). In order to achieve the purpose of uploading any number of pictures (within 4 pictures), the array_filter() function and callback function are used to remove empty elements in the array during the processing of uploaded files.

The array_filter() function uses a callback function to filter units in the array. The syntax is as follows:

Copy code The code is 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 will be included in the returned result array, and the key names of the array will remain unchanged.

Note: Do not modify the array in the callback function, for example, add or delete elements in the array. Once the array changes, the use of this function will be meaningless. If no callback() function is provided, array_filter() will delete all elements in the input that have a value of FALSE.
The callback function defined in this example is check(), which is used to verify whether the element value in the array is empty. Its syntax is as follows:

Copy code The code is as follows:
function check($var){//Verify whether the return value of the array is empty
return($var!="");
}

Note: To implement multiple image uploads through the POST method, when creating a form, the enctype="multipart/form-data" attribute must be specified. If you want to control the size of the uploaded file through the value of the hidden field MAX_FILE_SIZE, then the hiding must be placed before the file field of the uploaded file, otherwise it will not work.

3. Design process

(1) Create 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 operation of multiple files. The key code is as follows:

Copy code The code is as follows:
















Content 1: ="30">
Content 2: ="30">



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

(3) Create the index.php file to obtain the data submitted in the form, store multiple files in the server, and store the names and storage paths of the files in the database. The code is as follows:

Copy code The code is as follows:
header("Content-type:text/html;charset=UTF-8"); //Set the 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 whether the return value of the array is empty
return($var!="");//If it is not empty, return the array element
}
$files=array_filter($_POST["files"],"check");//Remove null values ​​in the array
$array=array_filter($_FILES["picture"]["name"],"check"); //Remove null values ​​in the array
foreach=($aarray as $key=>value){ //Loop to read the data in the array
$path='upfile/'.time().$key.strtolower(strstr($value,".")); //Define the upload file storage location
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"<script><br> alert('Image uploaded successfully');window.location.href='index.html';</script>";
}
?>

4. Additional skills

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

Copy code The code is as follows:
#LoadModule rewrite_module modules/mod_rewrite.so

Start this item by removing the "#" in front of it.
Then, search the httpd.conf file, find the AllowOverride item in it, and change its values ​​to All. Save and restart the Apache server for the changes to take effect.
Finally, create an .htaccess file in the instance root directory to hide the PHP file suffix. The code of the .htaccess file is as follows:

Copy code The code is 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]

Use regular expressions to match the file suffix and the passed parameters to complete the hiding operation of the PHP file suffix.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/902783.htmlTechArticlePHP uploads multiple files to the server instance. This example describes the implementation method of PHP uploading multiple files to the server. For multiple files to be uploaded to the server at the same time, we need to...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template