PHP头条
热点:

php 实现多文件上传程序代码


php文件上传与多文件上传其它没有多大的区别,多文件上传只是我们把表单名改成数组形式,而获取利用foreach遍历就可以实现多文件上传了,动态多文件上传只要在js加一个动态增加多文件上传框,同样在php处理时就遍历一下数组就成了。

最简单的实例如下

 代码如下 复制代码

<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");
        }
}
?>

 

下面分享其它朋友的例子
例1

 代码如下 复制代码

<?
//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;
}
?>
<html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<title>多个文件上传</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 file have been selected");
return false;
}
</script>
</head>
<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="35"><td align="right" valign="bottom">多文件上传 </td></tr>
<tr><td align="center" valign="top">
<table class="Transex" border="0" cellspacing="0" cellpadding="0" style="WIDTH: 360px">
<tr style="HEIGHT: 10px" >
<td style="WIDTH: 5px"></td>
<td colspan="2"></td>
<td style="WIDTH: 5px"></td>
</tr>
<tr>
<td></td>
<td nowrap><label class="FieldLabel"> 文件1</label></td>
<td><input type="file" class="TxtInput" tabindex="1" name="afile1" style="WIDTH: 282px"></td>
<td></td>
</tr>
<tr>
<td></td>
<td nowrap><label class="FieldLabel"> 文件2</label></td>
<td><input type="file" class="TxtInput" tabindex="2" name="afile2" style="WIDTH: 282px"></td>
<td></td>
</tr>
<tr>
<td></td>
<td nowrap><label class="FieldLabel"> 文件3</label></td>
<td><input type="file" class="TxtInput" tabindex="3" name="afile3" style="WIDTH: 282px"></td>
<td></td>
</tr>
<tr>
<td></td>
<td nowrap><label class="FieldLabel"> 文件4</label></td>
<td><input type="file" class="TxtInput" tabindex="4" name="afile4" style="WIDTH: 282px"></td>
<td></td>
</tr>
<tr>
<td></td>
<td nowrap><label class="FieldLabel"> 文件5</label></td>
<td><input type="file" class="TxtInput" tabindex="5" name="afile5" style="WIDTH: 282px"></td>
<td></td>
</tr>
<tr>
<td></td>
<td nowrap><label class="FieldLabel"> 文件6</label></td>
<td><input type="file" class="TxtInput" tabindex="6" name="afile6" style="WIDTH: 282px"></td>
<td></td>
</tr>
<tr>
<td></td>
<td nowrap><label class="FieldLabel"> 文件7</label></td>
<td><input type="file" class="TxtInput" tabindex="7" name="afile7" style="WIDTH: 282px"></td>
<td></td>
</tr>
<tr>
<td></td>
<td nowrap><label class="FieldLabel"> 文件8</label></td>
<td><input type="file" class="TxtInput" tabindex="8" name="afile8" style="WIDTH: 282px"></td>
<td></td>
</tr>
<tr style="HEIGHT: 5px">
<td style="WIDTH: 5px">
<td style="WIDTH: 350px" colspan="2"><hr width="100%"></td>
<td style="WIDTH: 5px"></td>
</tr>
<tr>
<td></td>
<td colspan="2" align="left">
<button tabindex="5" class="headbtn" align="center" name="btnOk" id="btnOk" accesskey="O">确定(<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();">取消(<ins>C</ins>)</button></td>
<td></td>
</tr>
<tr style="HEIGHT: 5px">
<td style="WIDTH: 5px">
<td style="WIDTH: 350px" colspan="2"></td>
<td style="WIDTH: 5px"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>


如果我们要动态不确定性的多文件上传怎么实现下面也有实例

文件上传代码

 代码如下 复制代码
view plaincopy to clipboardprint?
<!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>文档上传</title>
</head>
<body>
<script language="javascript"><!--
动态添加文件选择控件-->
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">
<!-- 将上传文件必须用post的方法和enctype="multipart/form-data" -->
<!-- 将本页的网址传给uploadfile.php-->
<input name="postadd" type="hidden" value="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER["PHP_SELF"]; ?>" />
<tr><td>文件上传列表
<input type="button" name="addfile" onclick="AddRow()" value="添加列表" /></td></tr>
<!-- filelist[]必须是一个数组-->
<tr><td><input type="file" name="filelist[]" size="50" /></td></tr>
</table>
<input type="submit" name="submitfile" value="提交文件" />
</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>文档上传</title>
</head>
<body>
<script language="javascript"><!--
动态添加文件选择控件-->
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">
<!-- 将上传文件必须用post的方法和enctype="multipart/form-data" -->
<!-- 将本页的网址传给uploadfile.php-->
<input name="postadd" type="hidden" value="<?php echo "http://".$_SERVER['HTTP_HOST'].$_SERVER["PHP_SELF"]; ?>" />
<tr><td>文件上传列表
<input type="button" name="addfile" onclick="AddRow()" value="添加列表" /></td></tr>
<!-- filelist[]必须是一个数组-->
<tr><td><input type="file" name="filelist[]" size="50" /></td></tr>
</table>
<input type="submit" name="submitfile" value="提交文件" />
</form>
</body>
</html>
提交文件代码
view plaincopy to clipboardprint?
<!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>文件上传结果</title>
</head>
<body>
<?php
if ($_POST["submitfile"]!="")
{
$Path="./".date('Ym')."/";
if (!is_dir($Path))//创建路径
{ mkdir($Path); }
echo "<div>";
for ($i=0;$i<count($filelist);$i++)
{ //$_FILES["filelist"]["size"][$i]的排列顺序不可以变,因为fileist是一个二维数组
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 "文件上传成功 文件类型:".$_FILES["filelist"]["type"][$i]." "."文件名:"
.$_FILES["filelist"]["name"][$i]."<br>"; }
else
{ echo "文件名:".$_FILES["filelist"]["name"][$i]."上传失败</br>"; }
}
}
echo "</div><br><a href="$postadd" href="$postadd">返回</a></div>";
}
?>
</body>
</html>

另:错误信息说明
从 PHP 4.2.0 开始,PHP 将随文件信息数组一起返回一个对应的错误代码。该代码可以在文件上传时生成的文件数组中的 error 字段中被找到,也就是 $_FILES['userfile']['error']。
UPLOAD_ERR_OK
其值为 0,没有错误发生,文件上传成功。
UPLOAD_ERR_INI_SIZE
其值为 1,上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。
UPLOAD_ERR_FORM_SIZE
其值为 2,上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。 UPLOAD_ERR_PARTIAL
其值为 3,文件只有部分被上传。
UPLOAD_ERR_NO_FILE
其值为 4,没有文件被上传。
UPLOAD_ERR_NO_TMP_DIR
其值为 6,找不到临时文件夹。PHP 4.3.10 和 PHP 5.0.3 引进。
UPLOAD_ERR_CANT_WRITE
其值为 7,文件写入失败。PHP 5.1.0 引进。
注意: 以上值在 PHP 4.3.0 之后变成了 PHP 常量。

www.phpzy.comtrue/php/6414.htmlTechArticlephp 实现多文件上传程序代码 php文件上传与多文件上传其它没有多大的区别,多文件上传只是我们把表单名改成数组形式,而获取利用foreach遍历就可以实现多文件上传了,动态多文件上...

相关文章

相关频道:

PHP之友评论

今天推荐