萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> PHP圖片、文件批量上傳代碼

PHP圖片、文件批量上傳代碼

   不管是文件還是圖片批量上傳我們第一個是在html中做name=userfile[]這種數組變量,在php接受中我們做for ($_i=0; $_i<$_cont; $_i++)遍歷這樣就可以實現文件批量上傳了,下面我來看一個實例

  例子

 代碼如下  

<?php
session_start();
?>
<!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=utf-8" />
<title>PHP文件批量上傳</title>
<style>
* {margin:0; padding:0; list-style:none;}
.content {width:400px; height:auto; margin:0 auto; margin-top:60px; padding-bottom:30px; background:#ffd3b6; border:dashed 1px #f90}
.content h1 { width:400px; height: 30px; line-height:30px; text-align:center; font-family:"微軟雅黑"; font-size:14px; color:#000}
.content .error {width:300px; height:auto; line-height:30px; text-align:center; margin:0 auto; color:#f00}
.content .con {width:340px; height:auto; margin:0 auto; font-size:12px;}
.content #file { width:280px; height:20px; border:solid 1px #ccc; background:#fff; margin:10px 0px 6px 0; font-size:12px;}
.content #send { width:60px; height:22px; border:solid 1px #ccc; background:#fff; font-size:12px; margin-top:10px;}
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script>
$(document).ready(function() {
$('#cont').val($('.file #file').size());
$('#send').eq(0).click(function() {
var filesize=$('.file #file').size();
$('.file').append("<input type='file' name='userfile[]' id='file'/>");
$('#cont').val(filesize+1);
});
});
</script>
</head>
<body>
<div>
<h1>PHP文件批量上傳</h1>
<div>
<div>
<?php
if ($_GET['up']==up) {
//防止重復提交
if ($_SESSION['file']==$_GET['irand']) {
$_cont=intval($_POST['cont']);   //將file框總數接收並轉換成整型
$_size=20000;                    //設置限制文件大小
$_dir='pdir/';                   //文件保存目錄
function size($_size) {
//判斷文件大小是否大於1024bit 如果大於,則將大小取值為KB,以此類推
if ($_size>1024*1024) {
return round($_size/1024/1024,2).' MB';
}else if ($_size>1024) {
$_size=$_size/1024;
return ceil($_size).'KB';
}else {
return $_size.' bit';
}
}
//設置上傳圖片的類型,設置圖片上傳大小
$_upfiles = array('image/jpeg','image/pjpeg','image/png','image/x-png','image/gif');
//利用for循環上傳文件
for ($_i=0; $_i<$_cont; $_i++) {
if (is_array($_upfiles)) {
if (!in_array($_FILES['userfile']['type'][$_i],$_upfiles)) {
exit('請上傳格式為:jpg,png,gif的文件<br /><a href="pupload.php">返回</a>');
}
}
//判斷文件大小
if ($_FILES['userfile']['size'][$_i]>$_size) {
exit('上傳文件不能超過:'.size($_size));
}
//檢測文件是否已經上傳
if ($_FILES['userfile']['error'][$_i]>0) {
switch ($_FILES['userfile']['error'][1]) {
case 1: echo '上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值';
break;
case 2: echo '上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值';
break;
case 3: echo '文件只有部分被上傳';
break;
case 4: echo '沒有文件被上傳';
break;
case 6: echo '找不到臨時文件夾';
break;
case 7: echo '文件寫入失敗';
break;
}
exit;
}
//獲取文件擴展名
if (!is_dir($_dir)) {
mkdir($_dir,0700);
}
//生成隨筆數
$_rand=mt_rand(0,100000);
//獲取文件擴展名
$_n=explode('.',$_FILES['userfile']['name'][$_i]);  //將文件名分割
$_file_len=count($_n);         //返回數組長度
//確保獲取的擴展名是最後一個.後面的
$_name=$_dir.time().'_'.$_rand.'.'.$_n[$_file_len-1];
//移動文件到指定的目錄
if (is_uploaded_file($_FILES['userfile']['tmp_name'][$_i])) {
if (!@move_uploaded_file($_FILES['userfile']['tmp_name'][$_i],$_name)) {
exit('文件移動失敗');
}else {
echo '文件上傳成功<br />';
echo '文件路徑:'.$_name.'<br />';
echo '文件大小:'.size(filesize($_name));
echo '<br /><a href="pupload.php">返回繼續上傳</a>';
}
}else {
exit('上傳的臨時文件不存在,無法將文件移動到指定文件夾');
}
}
//銷毀session變量,有幾種方法
//第一種,銷毀所有session變量:session_destroy();
//第二種:銷毀單個如:$_SESSION['file']=''
session_destroy();
exit;

}else {
exit('您已經提交過了,不能重復提交<br /><a href="pupload.php">返回</a>');
}
}
?>
</div>
<?php $_irand=mt_rand(0,1000000); $_SESSION['file']=$_irand; ?>
<form action="?up=up&irand=<?php echo $_irand; ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<input type="hidden" name="cont" value="" id="cont" />
<div>
<input type="file" name="userfile[]" id="file"/>
<input type="file" name="userfile[]" id="file"/>

copyright © 萬盛學電腦網 all rights reserved