php multi file upload function implementation principle and code

  • 2020-06-01 08:23:11
  • OfStack

Today, I did a little research on the uploading function of multiple pictures. I sorted out the following code, which is convenient for me to use and communicate with you in the future
1. upload.html page, which begins with input type=file file:
 
<html> 
<head> 
<title></title> 
</head> 
<body> 
<form enctype="multipart/form-data" action="do_upload.php" method="POST"> 
<!-- In the form enctype="multipart/form-data" Set the form MIME Encoding. By default, the encoding format is application/x-www-form-urlencoded , cannot be used for file upload; Only use multipart/form- data To complete the transfer of file data, do the following .--> 
<fieldset> 
<legend> Image upload </legend> 
 The first 1 image <input name="userfile" type="file"><br> 
 The first 2 image <input name="userfile" type="file"><br> 
 The first 3 image <input name="userfile" type="file"><br> 
 The first 4 image <input name="userfile" type="file"><br> 
 The first 5 image <input name="userfile" type="file"><br> 
<!-- Pass the data as an array --> 
<input type="submit" value="Send File"> 
</fieldset> 
</form> 
</body> 
</html> 

2. do_upload. php page, that is, the page for handling multiple file uploads:
 
<?php 
// Global array $_FILES 
//$_FILES['userfile']['tmp_name'] The file in web The location of temporary storage in the server  
//$_FILES['userfile']['name'] The name of the file in the user's system  
//$_FILES['userfile']['size'] The size of the file in bytes  
//$_FILES['userfile']['type'] Of the file MIME Type, text/plain,image/gif 
//$_FILES['userfile']['error'] Error codes associated with file upload  
?> 
<?php 
// with for Loop to get the data passed, yes 1 a 3 D data  
for ($i=0;$i<count($_FILES['userfile']['tmp_name']);$i++) 
{ 
$upfile=$new_folder."/".$_FILES['userfile']['name'][$i];// This can be modified according to your own needs  
if(move_uploaded_file($_FILES['userfile']['tmp_name'][$i],$upfile)){ 
echo " The first ".($i+1)." The picture was uploaded successfully <br>"; 
} 
else{ 
echo " The first ".($i+1)." I can't upload a picture <br>"; 
} 
} 
?> 

Related articles: