index.php页面表单页面
<form action="test.php" method="post" enctype="multipart/form-data">
name:<input type="text" name="username" value="" /><br/>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"/>
up pic:<input type="file" name="picture[]" value=""/><br/>
up pic:<input type="file" name="picture[]" value=""/><br/>
up pic:<input type="file" name="picture[]" value=""/><br/>
<input type="submit" value="upload" /><br/>
</form>
test.php页面 处理提交信息页面
<?php
header('content-type:text/html;charset=utf-8');
//var_dump($_POST);
//var_dump($_FILES);
/*
* 单个文件上传
* array (size=2)
'username' => string 'yang' (length=4)
'MAX_FILE_SIZE' => string '1000000' (length=7)
array (size=1)
'picture' =>
array (size=5)
'name' => string 'PHP.jpg' (length=7)
'type' => string 'image/jpeg' (length=10)
'tmp_name' => string 'D:\wamp\tmp\php7C.tmp' (length=21)
'error' => int 0
'size' => int 279468
*
*多个文件上传
*array (size=2)
'username' => string 'xiaohua' (length=7)
'MAX_FILE_SIZE' => string '1000000' (length=7)
array (size=1)
'picture' =>
array (size=5)
'name' =>
array (size=3)
0 => string 'PHP.jpg' (length=7)
1 => string 'PHP.jpg' (length=7)
2 => string 'PHP.jpg' (length=7)
'type' =>
array (size=3)
0 => string 'image/jpeg' (length=10)
1 => string 'image/jpeg' (length=10)
2 => string 'image/jpeg' (length=10)
'tmp_name' =>
array (size=3)
0 => string 'D:\wamp\tmp\php89.tmp' (length=21)
1 => string 'D:\wamp\tmp\php8A.tmp' (length=21)
2 => string 'D:\wamp\tmp\php8B.tmp' (length=21)
'error' =>
array (size=3)
0 => int 0
1 => int 0
2 => int 0
'size' =>
array (size=3)
0 => int 279468
1 => int 279468
2 => int 279468
*
*/
$num = count($_FILES['picture']['name']);
for($i=0;$i<$num;$i++){//设置多个文件上传
//第一步:判断错误
if($_FILES['picture']['error'][$i]>0){
switch ($_FILES['picture']['error'][$i]){
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;
default:
echo '未知错误';
}
//exit;
continue;
}
//第二步 判断类型
$arr = explode(".",basename($_FILES['picture']['name'][$i]));
$ext = array_pop($arr);
$allowType = array("gif","png","jpg","jpeg");
if(!in_array($ext, $allowType)){
echo '上传的类型不合法';
exit;
}
//第三步 判断大小
$maxsize = 1000000;
if($_FILES['picture']['size'][$i]>$maxsize){
echo "上传的文件超过最大存储值{$maxsize}字节";
exit;
}
//第四步 上传后的文件名 一定要设置随机文件名
$tmpfile = $_FILES['picture']['tmp_name'][$i];
$destname = "./uploads/".date('YmdHis').rand(99,99999).".".$ext;//设置随机文件名
//将临时目录下的上传的文件,复制到指定的新目录,就算上传成功。
if(move_uploaded_file($tmpfile, $destname)){
echo "上传成功!";
}else{
echo "上传失败!";
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章