<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>新建网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<script type="text/javascript">
</script>
<style type="text/css">
</style>
</head>
<body>
<h2>多文件上传案例</h2>
<form action="07.php" method="post" enctype="multipart/form-data">
用户名:<input type="text" name="username" value="张三" /><br />
头像:<input type="file" name="avatar" value="" /><br />
艳照:<input type="file" name="hotpic" value="" /><br />
简历:<input type="file" name="jianli" value="" /><br />
<input type="submit" value="提交" />
</form>
</body>
</html>
<?php
/***
多文件上传
***/
/*
计算并创建目录
*/
function mk_dir() {
$dir = date('md/i',time());
if(is_dir('./' . $dir)) {
return $dir;
} else {
mkdir('./' . $dir,0777,true);
return $dir;
}
}
function getExt($file) {
$tmp = explode('.',$file);
return end($tmp);
}
function randName() {
$str = 'abcdefghijkmnpqrstuvwxzy23456789';
return substr(str_shuffle($str),0,6);
}
//print_r($_FILES);
foreach($_FILES as $k=>$v) {
// 拼接文件路径
$path = './' . mk_dir() . '/' . randName() . '.' . getExt($v['name']);
if($v['error'] != 0) {
echo $k,'上传失败';
echo '错误代码是',$v['error'],'<br />';
continue;
}
// 移动
if(move_uploaded_file($v['tmp_name'],$path)) {
echo $k,'上传成功,<br />';
} else {
echo 'FAIL';
}
}