PHP Multiple files uploaded to server instance

  • 2021-07-24 10:17:18
  • OfStack

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

1. Example description

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

2. Key technologies

The key of multi-file uploading is how to define the names of uploaded file elements and how to judge 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 upload any number of pictures (less than 4 pictures), array_filter () function and callback function are applied to remove the empty elements in the array during the processing of uploaded files.

The array_filter () function, which filters the cells in the array with a callback function, has the following syntax:

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

In the callback function do not modify the array operation, for example, add or delete the elements of the array, if the 1-denier array changes, then the use of this function is meaningless. If the callback () function is not provided, array_filter () deletes all FALSE equivalent elements in input.
The callback function defined in this example is check (), which is used to verify that the element values in the array are null, and its syntax is as follows:

function check($var){// Verify that the return value of the array is empty 
return($var!="");
}

POST method to achieve multi-picture upload, in the creation of form form, must specify enctype= "multipart/form-data" attribute. If you want to control the size of the uploaded file by the value of the hidden field MAX_FILE_SIZE, you must place the hiding before the file field of the uploaded file, otherwise it will not work.

3. Design process

(1) Create the index. php file. Add the form, set the file field, submit the button, use the POST method, set enctype= "multipart/form-data", submit the data to index_ok. php page, and complete the upload operation of multiple files. The key codes are 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">&nbsp;&nbsp;&nbsp;&nbsp;
 <input type="image"name="imgeField2"src="images/bg_11.jpg"></td>
</tr>
</form>
 </table>

(2) Connect the database in index. php file, read the data stored in the database, and realize the paging output of the uploaded file. Please refer to the relevant contents in the CD for the code.

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

<?php
header("Content-type:text/html;charset=UTF-8"); // Set the file encoding format
include"conn/conn.php"; // Include database link files
if($_POST[files]!=""){
if(! is_dir("./upfile")){
mkdir("./upfile");// Create Upload File Storage Folder
}
$data=date("Y-m-d H:m:s");// Defining Time
function check($var){ // Verify that the return value of the array is empty
 return($var!="");// Returns array elements if not empty
}
$files=array_filter($_POST["files"],"check");// Strip empty values from an array
$array=array_filter($_FILES["picture"]["name"],"check"); // Strip empty values from an array
foreach=($aarray as $key=>value){ // Loop to read the data in the array
 $path='upfile/'.time().$key.strtolower(strstr($value,".")); // Define the storage location of uploaded files
 move_uploaded_file($_FILES["picture"]["tmp_name"][$key],$path);// Perform an 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(' The picture was uploaded successfully ');window.location.href='index.html';</script>";
 }
 ?>

4. Additional skills

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

#LoadModule rewrite_module modules/mod_rewrite.so

Remove the "#" before the item and start the item.
Then, look for the httpd. conf file, find the AllowOverride entries, and change their values to All. Save and restart the Apache server for the changes to take effect.
Finally, the. htaccess file is created in the root directory of the instance to hide the suffix of the PHP file. The code for the. htaccess file 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]

The suffix of the PHP file can be hidden by matching the suffix of the file with the passed parameters through the regular expression.

I hope this article is helpful to everyone's PHP programming.


Related articles: