file.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>fileupload</title>
</head>
<body>
<div>
<form name="form" action="upload.php" method="post" enctype="multipart/form-data">
姓名:<input type="text" name="name" /><br/>
头像1:<input type="file" name="pic[]" /><br/>
头像2:<input type="file" name="pic[]" /><br/>
<input type="submit" value="submit" />
</form>
</div>
</body>
</html>
upload.php
<?php
function getDir(){
$dir=date('Y/m/d',time());
if(is_dir('./'.$dir)){
return $dir;
}else{
mkdir('./'.$dir,0777,true);
return $dir;
}
}
function getName(){
$str='abcdefg2345678';
return substr(str_shuffle($str),0,6);
}
function getExt($file){
$str=explode('.', $file);
return end($str);
}
//单文件上传
// if($_FILES['pic']['error']!=0){
// echo 'upload fail';
// exit;
// }
// $path='./'.getDir().'/'.getName().'.'.getExt($_FILES['pic']['name']);
// if(move_uploaded_file($_FILES['pic']['tmp_name'], $path)){
// echo "upload success";
// }else{
// echo "upload fail";
// }
foreach ($_FILES['pic']['name'] as $k => $v) {
if($_FILES['pic']['error'][$k]!=0){
echo 'upload fail';
exit;
}
$path='./'.getDir().'/'.getName().'.'.getExt($v);
if(move_uploaded_file($_FILES['pic']['tmp_name'][$k], $path)){
echo "upload success<br />";
}else{
echo "upload fail";
}
}
上传配置php.ini File Uploads
upload_tmp_dir = "d:/wamp/tmp"
file_uploads = On
upload_max_filesize = 2M
Post_max_size=8M
$_FILES 参数
Array
(
[pic] => Array
(
[name] => Array
(
[0] => 58_P_20140218032659_78.jpg
[1] => 59_P_20140219021347_53.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
)
[tmp_name] => Array
(
[0] => D:\wamp\tmp\php7F00.tmp
[1] => D:\wamp\tmp\php7F11.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 67957
[1] => 162200
)
)
)