php Multiple File and Picture Upload Example Explanation

  • 2021-07-26 07:07:18
  • OfStack

This paper describes the method of uploading multiple files and pictures of php with examples. Share it for your reference. The specific implementation method is as follows:

Multiple file upload is on the basis of single file upload using traversal array traversal form array and then upload files to the server one by one, let's look at a simple example of multiple file upload

Multiple file uploads and single file uploads are handled in the same way, only several input forms of type "file" are provided at the client, and different attribute values of "name" are specified. For example, in the following code, the user can select three local files at the same time and upload them to the server. The client form is as follows:

<html>
<head><title> Multiple file upload form </title></head>
<body>
<form action="mul_upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
Select a file 1 : <input type="file" name='myfile[]'><br>
Select a file 2 : <input type="file" name='myfile[]'><br>
Select a file 3 : <input type="file" name='myfile[]'><br>
<input type="submit" value=" Upload a file ">
</form>
</body>
</html>

In the above code, the forms of three file types are organized in an array from 1. When the above form taught the script file mul_upload. php of PHP, the global array $_ FILES was also used on the server side to store all the above files, but $_ FILES has been converted from a 2-dimensional array to a 3-dimensional array, so that it can store the information of multiple uploaded files. In the script file mul_upload. php, use the print_r () function to output the contents of the $_FILES array as follows:
<?php
// Print 3 Dimensional array $_FILES View the contents in the 1 The structure of storing uploaded files under
print_r($_FILES);
?>

When you select 3 local files to submit, the output is as follows

Array(
    [myfile]=>Array(
        [name]=>Array(              //$_FILES["myfile"]["name"] Store the contents of all uploaded files
            [0]=>Rav.ini         //$_FILES["myfile"]["name"][0] No. 1 1 The name of the uploaded file
            [1]=>msgsocm.log     //$_FILES["myfile"]["name"][1] No. 1 2 The name of the uploaded file
            [2]=>NOTEPAD.EXE)        //$_FILES["myfile"]["name"][2] No. 1 3 The name of the uploaded file
        [type]=>Array(               //$_FILES["myfile"]["type"] Stores the types of all uploaded files
            [0]=>application/octet-stream          //$_FILES["myfile"]["type"][0] No. 1 1 Types of files uploaded
            [1]=>application/octet-stream          //$_FILES["myfile"]["type"][1] No. 1 2 Types of files uploaded
            [2]=>application/octet-stream)         //$_FILES["myfile"]["type"][2] No. 1 3 Types of files uploaded
        [tmp_name]=>Array(
            [0]=>C:/WINDOWS/Temp/phpAF.tmp
            [1]=>C:/WINDOWS/Temp/phpB0.tmp
            [2]=>C:/WINDOWS/Temp/phpB1.tmp)
        [error]=>Array(
            [0]=>0
            [1]=>0
            [2]=>0)
        [size]=>Array(
            [0]=>64
            [1]=>1350
            [2]=>66560))
)

By outputting the value of the $_FILES array, we can see that the situation when handling multiple file uploads is similar to that when uploading a single file, except that the structure of the $_FILES array is slightly different. In this way, a larger number of file uploads can be supported.
Examples are as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> Document upload </title>
</head>
<body>
<script language="javascript"><!--
Dynamic Adding File Selection Controls -->
function AddRow()
{
var eNewRow = tblData.insertRow();
for (var i=0;i<1;i++)
{
var eNewCell = eNewRow.insertCell();
eNewCell.innerHTML = "<tr><td><input type='file' name='filelist[]' size='50'/></td></tr>";
}
}
// --></script>
<form name="myform" method="post" action="uploadfile.php" enctype="multipart/form-data" >
<table id="tblData" width="400" border="0">
<!-- To upload a file, you must use the post Method and method of enctype="multipart/form-data" -->
<!-- Pass the URL of this page to uploadfile.php-->
<input name="postadd" type="hidden" value="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER["PHP_SELF"]; ?>" />
<tr><td> File upload list
<input type="button" name="addfile" onclick="AddRow()" value=" Add a list " /></td></tr>
<!-- filelist[] Must be 1 Array of numbers -->
<tr><td><input type="file" name="filelist[]" size="50" /></td></tr>
</table>
<input type="submit" name="submitfile" value=" Submission of documents " />
</form>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> Document upload </title>
</head>
<body>
<script language="javascript"><!--
Dynamic Adding File Selection Controls -->
function AddRow()
{
var eNewRow = tblData.insertRow();
for (var i=0;i<1;i++)
{
var eNewCell = eNewRow.insertCell();
eNewCell.innerHTML = "<tr><td><input type='file' name='filelist[]' size='50'/></td></tr>";
}
}
// --></script>
<form name="myform" method="post" action="uploadfile.php" enctype="multipart/form-data" >
<table id="tblData" width="400" border="0">
<!-- To upload a file, you must use the post Method and method of enctype="multipart/form-data" -->
<!-- Pass the URL of this page to uploadfile.php-->
<input name="postadd" type="hidden" value="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER["PHP_SELF"]; ?>" />
<tr><td> File upload list
<input type="button" name="addfile" onclick="AddRow()" value=" Add a list " /></td></tr>
<!-- filelist[] Must be 1 Array of numbers -->
<tr><td><input type="file" name="filelist[]" size="50" /></td></tr>
</table>
<input type="submit" name="submitfile" value=" Submission of documents " />
</form>
</body>
</html>

Submit file code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> File upload results </title>
</head>
<body>
<?php
if ($_POST["submitfile"]!="")
{
$Path="./".date('Ym')."/";
if (!is_dir($Path))// Create path
{ mkdir($Path); }
echo "<div>";
for ($i=0;$i<count($filelist);$i++)
{ //$_FILES["filelist"]["size"][$i] The order of the arrangement cannot be changed because fileist Yes 1 A 2 Dimensional array
if ($_FILES["filelist"]["size"][$i]!=0)
{
$File=$Path.date('Ymdhm')."_".$_FILES["filelist"]["name"][$i];
if (move_uploaded_file($_FILES["filelist"]["tmp_name"][$i],$File))
{ echo " File uploaded successfully File type :".$_FILES["filelist"]["type"][$i]." "." Filename :"
.$_FILES["filelist"]["name"][$i]."<br>"; }
else
{ echo " Filename :".$_FILES["filelist"]["name"][$i]." Upload failed </br>"; }
}
}
echo "</div><br><a href="$postadd" href="$postadd"> Return </a></div>";
}
?>
</body>
</html>

The above example is based on js to dynamically increase the upload file box, so as to achieve the function of multi-file upload.

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


Related articles: