php多個文件及圖片上傳實例詳解
本文實例講述了php多個文件及圖片上傳的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
多個文件上傳是在單文件上傳的基礎上利用遍歷數(shù)組的方式進行遍歷表單數(shù)組然后把文件一個個上傳到服務器上了,下面就來看一個簡單多個文件上傳實例
多個文件上傳和單獨文件上傳的處理方式是一樣的,只需要在客戶端多提供幾個類型為“file”的輸入表單,并指定不同的“name”屬性值。例如,在下面的代碼中,可以讓用戶同時選擇三個本地文件一起上傳給服務器,客戶端的表單如下所示:
<head><title>多個文件上傳表單</title></head>
<body>
<form action="mul_upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
選擇文件1:<input type="file" name='myfile[]'><br>
選擇文件2:<input type="file" name='myfile[]'><br>
選擇文件3:<input type="file" name='myfile[]'><br>
<input type="submit" value="上傳文件">
</form>
</body>
</html>
在上面的代碼中,將三個文件類型的表單以數(shù)組的形式組織在一起。當上面的表單教給PHP的腳本文件mul_upload.php時,在服務器端同樣使用全局數(shù)組$_FILES存儲所有上述文件的信息,但$_FILES由二維數(shù)組已經(jīng)轉變?yōu)槿S數(shù)組,這樣就可以存儲多個上傳文件的信息。在腳本文件mul_upload.php中,使用print_r()函數(shù)將$_FILES數(shù)組中的內容輸出,代碼如下所示:
//打印三維數(shù)組$_FILES中的內容,查看一下存儲上傳文件的結構
print_r($_FILES);
?>
當選擇三個本地文件提交后,輸出結果如下所示
[myfile]=>Array(
[name]=>Array( //$_FILES["myfile"]["name"]存儲所有上傳文件的內容
[0]=>Rav.ini //$_FILES["myfile"]["name"][0]第一個上傳文件的名稱
[1]=>msgsocm.log //$_FILES["myfile"]["name"][1]第二個上傳文件的名稱
[2]=>NOTEPAD.EXE) //$_FILES["myfile"]["name"][2]第三個上傳文件的名稱
[type]=>Array( //$_FILES["myfile"]["type"]存儲所有上傳文件的類型
[0]=>application/octet-stream //$_FILES["myfile"]["type"][0]第一個上傳文件的類型
[1]=>application/octet-stream //$_FILES["myfile"]["type"][1]第二個上傳文件的類型
[2]=>application/octet-stream) //$_FILES["myfile"]["type"][2]第三個上傳文件的類型
[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))
)
通過輸出$_FILES數(shù)組的值可以看到,處理多個文件的上傳和單個文件上傳時的情況一樣的,只是$_FILES數(shù)組的結構形式略有不同。通過這種方式可以支持更多數(shù)量的文件上傳。
例子如下:
<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"><!--
動態(tài)添加文件選擇控件-->
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" -->
<!-- 將本頁的網(wǎng)址傳給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[]必須是一個數(shù)組-->
<tr><td><input type="file" name="filelist[]" size="50" /></td></tr>
</table>
<input type="submit" name="submitfile" value="提交文件" />
</form>
</body>
</html>
<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"><!--
動態(tài)添加文件選擇控件-->
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" -->
<!-- 將本頁的網(wǎng)址傳給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[]必須是一個數(shù)組-->
<tr><td><input type="file" name="filelist[]" size="50" /></td></tr>
</table>
<input type="submit" name="submitfile" value="提交文件" />
</form>
</body>
</html>
提交文件代碼
<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))//創(chuàng)建路徑
{ mkdir($Path); }
echo "<div>";
for ($i=0;$i<count($filelist);$i++)
{ //$_FILES["filelist"]["size"][$i]的排列順序不可以變,因為fileist是一個二維數(shù)組
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>
上面例子基于 js來動態(tài)增加上傳文件框了,從而達到多文件上傳的功能。
希望本文所述對大家的PHP程序設計有所幫助。
相關文章
在PHP中實現(xiàn)Javascript的escape()函數(shù)代碼
在 Javascript 中,可以利用 escape/unescape() 和 eval_r() 函數(shù)進行簡單的轉碼處理,讓普通的 URL 網(wǎng)址看起來比較怪異,使那些令人討厭的搜索爬蟲無法辨認你希望隱藏的資源。2010-08-08
PHP中文處理 中文字符串截取(mb_substr)和獲取中文字符串字數(shù)
PHP中文處理 中文字符串截取(mb_substr)和獲取中文字符串字數(shù),需要的朋友可以參考下。2011-11-11
Ajax+Jpgraph實現(xiàn)的動態(tài)折線圖功能示例
這篇文章主要介紹了Ajax+Jpgraph實現(xiàn)的動態(tài)折線圖功能,結合實例形式分析了ajax結合jpgraph.php類庫繪制動態(tài)折線圖的相關操作技巧,需要的朋友可以參考下2019-02-02
比較全的PHP 會話(session 時間設定)使用入門代碼
由于 Session 是以文本文件形式存儲在服務器端的,所以不怕客戶端修改 Session 內容。實際上在服務器端的 Session 文件,PHP 自動修改 Session 文件的權限,只保留了系統(tǒng)讀和寫權限,而且不能通過 ftp 修改,所以安全得多。2008-06-06
Yii 使用intervention/image拓展實現(xiàn)圖像處理功能
這篇文章主要介紹了Yii 使用intervention/image拓展實現(xiàn)圖像處理功能,需要的朋友可以參考下2019-06-06

