PHP implementation of multiple file upload program code

Source: Internet
Author: User
Tags file upload html form xmlns

The simplest example is the following

The code is as follows Copy Code

<form action= "" method= "Post" enctype= "Multipart/form-data" >
<p>pictures:
<input type= "File" Name= "pictures[]"/>
<input type= "File" Name= "pictures[]"/>
<input type= "File" Name= "pictures[]"/>
<input type= "Submit" value= "Send"/>
</p>
</form>

<?php
foreach ($_files["Pictures"] ["error"] as $key => $error) {
if ($error = = UPLOAD_ERR_OK) {
$tmp _name = $_files["Pictures" ["Tmp_name"] [$key];
$name = $_files["Pictures" ["Name"] [$key];
Move_uploaded_file ($tmp _name, "data/$name");
}
}
?>

Let's share examples of other friends
Example 1

The code is as follows Copy Code

?
filename:multi_upload.php
if ($ifupload)
{
$path =addslashes (dirname ($PATH _translated)). " \upload\ ";
For ($i =1 $i <=8; $i + +)
{
$files = "Afile". $i;
if (${$files}!= "None")
{
if (Copy (${$files}, $path. ${$files. " _name "}))
{
}
}
}
Print "<b>you have uploaded files successfully</b><br>";
Print "<a href=" multi_upload.php ">Return</a>";
Exit
}
?>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<meta name= "generator" content= "Microsoft Visual Studio 6.0" >
<title> multiple file uploads </title>
<style type= "Text/css" >
<!--
Body
{
padding-right:0px;
margin-top:0px;
padding-left:0px;
font-size:8px;
margin-left:0px;
Cursor:default;
Color:black;
margin-right:0px;
padding-top:0px;
font-family:arial;
Background-color:transparent;
Text-align:center
}
. Txtinput
{
font-size:8pt;
width:100%;
Cursor:default;
Color:black;
font-family:arial;
height:21px;
Background-color:white;
Text-align:left
}
. Fieldlabel
{
Font-weight:normal;
font-size:9pt;
width:100%;
Color:black;
font-family:arial;
Background-color:transparent;
Text-align:left
}
. Headbtn
{
Border-right:black 1px solid;
Border-top:white 1px solid;
font-size:8pt;
Overflow:hidden;
Border-left:white 1px solid;
width:70px;
Color:black;
Border-bottom:black 1px solid;
font-family:arial;
height:21px;
Background-color: #8e8dcd;
Text-align:center
}
. Transex
{
Border-right:black 1px solid;
padding-right:8px;
Border-top:white 1px solid;
padding-left:8px;
font-size:8pt;
padding-bottom:3px;
Border-left:white 1px solid;
width:720px;
padding-top:3px;
Border-bottom:black 1px solid;
font-family:arial;
Background-color: #c0c0c0;
Text-align:center
}
-->
</style>
<script language= "JavaScript" >
function Window.onload ()
{
DOCUMENT.FORMS[0].BTNOK.ONCLICK=BTN_OK;
}
function Btn_ok ()
{
for (Var i=1;i<=8;i++)
{
if (eval ("Document.forms[0].afile" +i+ ". value!="))
Document.forms[0].submit ();
return true;
}
Alert ("None of the file have been selected");
return false;
}
</script>
<body>
<form method= "POST" action= "multi_upload.php" name= "Frmupload" enctype= "Multipart/form-data" >
<table id= "Divcontainer" style= "height:100%; width:380 "border=" 0 ">
<tr height= "&GT;&LT;TD" align= "right" valign= "bottom" > Multi-File Upload </td></tr>
&LT;TR&GT;&LT;TD align= "center" valign= "Top" >
<table class= "Transex" border= "0" cellspacing= "0" cellpadding= "0" style= "width:360px" >
<tr style= "height:10px" >
&LT;TD style= "width:5px" ></td>
&LT;TD colspan= "2" ></td>
&LT;TD style= "width:5px" ></td>
</tr>
<tr>
<td></td>
&LT;TD nowrap><label class= "Fieldlabel" > File 1</label></td>
<td><input type= "File" class= "Txtinput" tabindex= "1" name= "Afile1" style= "width:282px" ></td>
<td></td>
</tr>
<tr>
<td></td>
&LT;TD nowrap><label class= "Fieldlabel" > File 2</label></td>
<td><input type= "File" class= "Txtinput" tabindex= "2" name= "Afile2" style= "width:282px" ></td>
<td></td>
</tr>
<tr>
<td></td>
&LT;TD nowrap><label class= "Fieldlabel" > File 3</label></td>
<td><input type= "File" class= "Txtinput" tabindex= "3" name= "Afile3" style= "width:282px" ></td>
<td></td>
</tr>
<tr>
<td></td>
&LT;TD nowrap><label class= "Fieldlabel" > File 4</label></td>
<td><input type= "File" class= "Txtinput" tabindex= "4" name= "Afile4" style= "width:282px" ></td>
<td></td>
</tr>
<tr>
<td></td>
&LT;TD nowrap><label class= "Fieldlabel" > File 5</label></td>
<td><input type= "File" class= "Txtinput" tabindex= "5" name= "Afile5" style= "width:282px" ></td>
<td></td>
</tr>
<tr>
<td></td>
&LT;TD nowrap><label class= "Fieldlabel" > File 6</label></td>
<td><input type= "File" class= "Txtinput" tabindex= "6" name= "Afile6" style= "width:282px" ></td>
<td></td>
</tr>
<tr>
<td></td>
&LT;TD nowrap><label class= "Fieldlabel" > File 7</label></td>
<td><input type= "File" class= "Txtinput" tabindex= "7" name= "Afile7" style= "width:282px" ></td>
<td></td>
</tr>
<tr>
<td></td>
&LT;TD nowrap><label class= "Fieldlabel" > File 8</label></td>
<td><input type= "File" class= "Txtinput" tabindex= "8" name= "Afile8" style= "width:282px" ></td>
<td></td>
</tr>
<tr style= "height:5px" >
&LT;TD style= "width:5px" >
&LT;TD style= "width:350px" colspan= "2" >&LT;TD style= "width:5px" ></td>
</tr>
<tr>
<td></td>
&LT;TD colspan= "2" align= "left" >
<button tabindex= "5" class= "HEADBTN" align= "center" name= "Btnok" id= "Btnok" accesskey= "O" > Ok (<ins>o</ ins>) </button>
<input type= "hidden" name= "Ifupload" value=1>
<button tabindex= "5" class= "HEADBTN" align= "center" name= "Btncancel" id= "Btncancel" accesskey= "C" onclick= " Window.close (); " > Cancellation (<ins>C</ins>) </button></td>
<td></td>
</tr>
<tr style= "height:5px" >
&LT;TD style= "width:5px" >
&LT;TD style= "width:350px" colspan= "2" ></td>
&LT;TD style= "width:5px" ></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>


If we want the dynamic uncertainty of the multiple file upload how to achieve the following also has an example

File Upload Code

The code is as follows Copy Code
View Plaincopy to Clipboardprint?
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Document Upload </title>
<body>
<script language= "JavaScript" ><!--
Dynamically adding a file selection control-->
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= '/></td></tr > ";
}
}
--></script>
<form name= "MyForm" method= "post" action= "uploadfile.php" enctype= "Multipart/form-data" >
<table id= "Tbldata" width= "border=" "0" >
<!--will upload the file must use the Post method and enctype= "Multipart/form-data"-->
<!--the Web 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 List"/></td></tr>
<!--filelist[] must be an array-->
<tr><td><input type= "File" name= "filelist[" "size="/></td></tr>
</table>
<input type= "Submit" Name= "Submitfile" value= "Submitting Documents"/>
</form>
</body>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Document Upload </title>
<body>
<script language= "JavaScript" ><!--
Dynamically adding a file selection control-->
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= '/></td></tr > ";
}
}
--></script>
<form name= "MyForm" method= "post" action= "uploadfile.php" enctype= "Multipart/form-data" >
<table id= "Tbldata" width= "border=" "0" >
<!--will upload the file must use the Post method and enctype= "Multipart/form-data"-->
<!--the Web 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 List"/></td></tr>
<!--filelist[] must be an array-->
<tr><td><input type= "File" name= "filelist[" "size="/></td></tr>
</table>
<input type= "Submit" Name= "Submitfile" value= "Submitting Documents"/>
</form>
</body>
Submit File Code
View Plaincopy to Clipboardprint?
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> File Upload Results </title>
<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] can not be arranged in order, because Fileist is a two-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 upload successful file type:". $_files["FileList"] ["type"] [$i]. " "." File name: "
. $_files["FileList" ["Name"] [$i]. " <br> "; }
Else
{echo "filename:". $_files["FileList"] ["name"] [$i]. " Upload Failure </br> "; }
}
}
echo "</div><br><a href=" $postadd "href=" $postadd "> Return </a></div>";
}
?>
</body>

Another: Error message description
Starting with PHP 4.2.0, PHP returns a corresponding error code along with the file information array. The code can be found in the Error field in the file array generated when the file is uploaded, i.e. $_files[' userfile ' [' Error '].
UPLOAD_ERR_OK
has a value of 0, no errors occurred, and the file was uploaded successfully.
Upload_err_ini_size
has a value of 1, and the uploaded file exceeds the value of the Upload_max_filesize option limit in php.ini.
Upload_err_form_size
has a value of 2, and the size of the uploaded file exceeds the value specified by the Max_file_size option in the HTML form. Upload_err_partial
Its value is 3 and the file is only partially uploaded.
Upload_err_no_file
has a value of 4 and no files are uploaded.
Upload_err_no_tmp_dir
has a value of 6 and cannot find a temporary folder. PHP 4.3.10 and PHP 5.0.3 introduced.
Upload_err_cant_write
has a value of 7, and the file write failed. PHP 5.1.0 introduced.
Note: The above values become PHP constants after PHP 4.3.0.

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.