PHP_简单多文件上传

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    
                )    
        )    
)


转载于:https://my.oschina.net/miaowang/blog/206498

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值