本文實例講述了php簡單實現(xiàn)批量上傳圖片的方法。分享給大家供大家參考,具體如下:
<?php
function upload_multi($path,$photo,$i){
$uploaddir = './'.$path;//文件存放目錄
if(!file_exists($uploaddir))//如果目錄不存在就新建
$uploaddir=mkdir($uploaddir);
$piece = explode('.',$photo['name'][$i]);
$uploadfile = $uploaddir . '/'.md5($piece[0]).'.'.$piece[1];
$result = move_uploaded_file($photo['tmp_name'][$i], $uploadfile);
if(!$result){
exit('上傳失敗');
}
return basename($uploadfile);
}
if($_POST['tijiao']){
extract($_POST);
$i=0;
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
upload_multi($email,$_FILES["pictures"],$i);
}
$i++;
}
}
?>
<script language="javascript">
function go_up(){
document.getElementById('new_up').innerHTML+='<input type="file" name="pictures[]" /><br>';
}
</script>
<form action="index.php" method="post" enctype="multipart/form-data">
<p>多圖片上傳</p><br>
<input type="file" name="pictures[]" /><br>
<div id="new_up"></div>
<input type="button"" name="add_img" value="新增上傳" onclick="go_up()"/><br>
<input type="submit" value="Send" name="tijiao"/><br>
</form>
希望本文所述對大家PHP程序設(shè)計有所幫助。