思想;把html的input標籤組織成一個數組,而後去重php
關鍵技術涉及的函數 is_dir mkdir move_uploaded_file()html
涉及的數組 預約義數組$_FILES數組
步驟一:檢查上傳文件夾是否存在,若是不存在就建立(注意!儘可能使用絕對路徑,不然可能上傳失敗)函數
$targetdir = "H:\\myphpproject\\myPHP\\upresource"; if (!is_dir($targetdir)){ mkdir($targetdir); }
step2: 把html的input標籤組織爲數組,關鍵在於name屬性要起作 xxxx[]的形式post
<form method="post" action="upfile.php" enctype="multipart/form-data">
<div class="media text-muted pt-3">
<input type="file" class="form-control" name="myfile[]" aria-label="Amount (to the nearest dollar)">
</div>
<div class="media text-muted pt-3">
<input type="file" class="form-control" name="myfile[]" aria-label="Amount (to the nearest dollar)">
</div>
<div class="media text-muted pt-3">
<input type="file" class="form-control" name="myfile[]" aria-label="Amount (to the nearest dollar)">
</div>
<br/>
<button type="submit" class="btn btn-success">提交</button>
</form>
step3:對上傳文件去重,並組織上傳文件的數組; 關鍵技術 array_unique編碼
$arrayfile = array_unique($_FILES['myfile']['name']);
step4:循環遍歷input標籤得到文件名,並和目標文件夾路徑組成上傳文件的絕對路徑spa
$path = $targetdir."\\".$v;
step5:循環遍歷每一個input標籤,上傳文件 關鍵技術:foreach ($arrayfile as $k=>$v);上傳用到 move_uploaded_file($_FILES[標籤數組名][‘tmp_name’][數組索引],$v),其中tmp_name是固定用法,沒必要深究;$v是上傳成功後,文件的絕對路徑名rest
$res是判斷上傳結果的布爾型變量code
foreach($arrayfile as $k=>$v) { $path = $targetdir."\\".$v; if ($v) { if(move_uploaded_file($_FILES['myfile']['tmp_name'][$k],$path)) { $res = TRUE; } else { $res=FALSE; } } }
總體既視感以下orm
html上傳文件部分 ----input標籤 type要設置成 file, enctype="multipart/form-data"不能省略
<form method="post" action="upfile.php" enctype="multipart/form-data">
<div class="media text-muted pt-3">
<input type="file" class="form-control" name="myfile[]" aria-label="Amount (to the nearest dollar)">
</div>
<div class="media text-muted pt-3">
<input type="file" class="form-control" name="myfile[]" aria-label="Amount (to the nearest dollar)">
</div>
<div class="media text-muted pt-3">
<input type="file" class="form-control" name="myfile[]" aria-label="Amount (to the nearest dollar)">
</div>
<br/>
<button type="submit" class="btn btn-success">提交</button>
</form>
整個上傳文件控制編碼部分
<?php $targetdir = "H:\\myphpproject\\myPHP\\upresource"; if (!is_dir($targetdir)){ mkdir($targetdir); } $arrayfile = array_unique($_FILES['myfile']['name']); $res = FALSE; foreach($arrayfile as $k=>$v) { $path = $targetdir."\\".$v; if ($v) { if(move_uploaded_file($_FILES['myfile']['tmp_name'][$k],$path)) { $res = TRUE; } else { $res=FALSE; } } } if ($res==TRUE) { echo "文件上傳成功"; } else { echo "文件上傳失敗"; } ?>