程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php 動態多文件上傳

php 動態多文件上傳

編輯:關於PHP編程

文件上傳代碼
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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved