1.单文件上传函数
upload.php
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<form action="doAction3.php" method="post" enctype="multipart/form-data">
选择上传的文件<input type="file" name="myfile"/><br/>
输入:<input type="text" name="mytext"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
doAction3.php
<?php
require_once 'uploadFun.php';
$fileInfo = $_FILES['myfile'];
$newName = uploadFile($fileInfo);
var_dump($newName);
uploadFun.php
<?php
header('Content-Type:text/html;charset=utf-8');
$fileInfo = $_FILES['myfile'];
function uploadFile($fileInfo,$flag=true, $uploadPath = './uploads/',$allowExt = array('jpeg','jpg','png','gif'),$maxSize = 2097152)
{
//判断错误号
if($fileInfo['error']>0){
switch($fileInfo['error'])
{
case 1:
$mes = "上传文件超过了PHP配置文件中upload_max_filesize选项的值";
break;
case 2:
$mes = "超过了表单max_file_size限制的大小";
break;
case 3:
$mes = "部分文件被上传";
break;
case 4:
$mes = "没有选择上传文件";
break;
case 6:
$mes = "没有找到临时目录";
break;
case 7:
case 8:
$mes = "系统错误";
break;
}
exit($mes);
}
//检测上传文件的类型
$ext = pathinfo($fileInfo['name'],PATHINFO_EXTENSION);
if(!in_array($ext,$allowExt)){
exit('非法文件类型');
}
//检测上传文件大小是否符合规范
if($fileInfo['size']>$maxSize){
exit('上传文件过大');
}
//检测图片是否为真实的图片类型
if($flag){
if(!getimagesize($fileInfo['tmp_name'])){
exit('不是真实的图片类型');
}
}
//检测是否通过 HTTP POST 上传的
if(!is_uploaded_file($fileInfo['tmp_name'])){
exit('文件不是通过 HTTP POST 方式上传的');
}
if(!file_exists($uploadPath)){
mkdir($uploadPath,0777,true);
chmod($uploadPath,0777);
}
$uniName = md5(uniqid(microtime(true),true)) . '.' . $ext;
$destination = $uploadPath . '/' . $uniName;
if(!@move_uploaded_file($fileInfo['tmp_name'],$destination)){
exit('文件上传失败');
}
//echo '文件上传成功';
return array(
'newName'=>$destination,
'size'=>$fileInfo['size'],
'type'=>$fileInfo['type']
);
}
2.多文件上传
myfile1 ,myfile2 ,myfile3 ,myfile4 ,返回二维数组
三维数组
index.php
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<form action="mediun.php" method="post" enctype="multipart/form-data">
请选择要上传的文件<input type="file" name="myfile[]"/><br/>
请选择要上传的文件<input type="file" name="myfile[]"/><br/>
请选择要上传的文件<input type="file" name="myfile2[]"/><br/>
请选择要上传的文件<input type="file" name="myfile2[]"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
medium.php
<?php
header('Content-Type:text/html;charset=utf-8');
require_once 'doAction.php';
$files = getFiles();
foreach($files as $fileInfo)
{
$res = uploadFile($fileInfo);
echo $res['mes'] . '<br/>';
$uploadFiles[] = $res['dest'];
}
$uploadFiles = array_values(array_filter($uploadFiles));
var_dump($uploadFiles);
doAction.php
<?php
//获取上传文件信息
function getFiles(){
$i=0;
foreach($_FILES as $file){
if(is_string($file['name'])){
$files[$i]=$file;
$i++;
}elseif(is_array($file['name'])){
foreach($file['name'] as $key=>$val){
$files[$i]['name']=$file['name'][$key];
$files[$i]['type']=$file['type'][$key];
$files[$i]['tmp_name']=$file['tmp_name'][$key];
$files[$i]['error']=$file['error'][$key];
$files[$i]['size']=$file['size'][$key];
$i++;//这里不能采用 $k 值,避免数组重复覆盖
}
}
}
return $files;
}
function uploadFile($fileInfo,$maxSize=50000000,$allowExt=array('jpg','png'),$flag=true,$path='./uploads',$res=array())
{
//检测上传文件大小
if($fileInfo['error'] == 0){
if($fileInfo['size']>$maxSize){
$res['mes'] = $fileInfo['name'] . '上传文件太大';
}
$ext = getExt($fileInfo['name']);
//检测上传文件类型
if(!in_array($ext,$allowExt)){
$res['mes'] = $fileInfo['name'] . '非法文件类型';
}
//检测是否为真实文件类型
if($flag){
if(!getimagesize($fileInfo['tmp_name'])){
$res['mes'] = $fileInfo['name'] . '不是真实图片';
}
}
//检测是否通过 HTTP POST 上传
if(!is_uploaded_file($fileInfo['tmp_name'])){
$res['mes'] = $fileInfo['name'] . '不是通过HTTP POST 上传的图片';
}
if($res) return $res;
if(!file_exists($path)){
mkdir($path,0777,true);
chmod($path,0777);
}
$uniName = getUniName();
$destination = $path . '/' . $uniName . '.' .$ext;
if(!move_uploaded_file($fileInfo['tmp_name'],$destination)){
$res['mes'] = $fileInfo['name'] . '文件移动失败';
}
$res['mes'] = $fileInfo['name'] . '文件上传成功';
$res['dest'] = $destination;
return $res;
}else{
//匹配错误信息
switch ($fileInfo ['error']) {
case 1 :
$res['mes'] = '上传文件超过了PHP配置文件中upload_max_filesize选项的值';
break;
case 2 :
$res['mes'] = '超过了表单MAX_FILE_SIZE限制的大小';
break;
case 3 :
$res['mes'] = '文件部分被上传';
break;
case 4 :
$res['mes'] = '没有选择上传文件';
break;
case 6 :
$res['mes'] = '没有找到临时目录';
break;
case 7 :
case 8 :
$res['mes'] = '系统错误';
break;
}
return $res;
}
}
function getExt($name)
{
return pathinfo($name,PATHINFO_EXTENSION);
}
function getUniName()
{
return md5(uniqid(microtime(true),true));
}