多图片上传(可判断是否重复上传图片)

function uploadFile($fileName)
{
global $empire, $dbtbpre;
if($fileName){

if ($_FILES['upfile']['name'][0] == "") {
exit("请上传文件!");
}//判断第一个文件名是否为空
$dir = $_SERVER['DOCUMENT_ROOT']."/ziyuan/wenjian/tupian/fengmiantu/".date('Y',time())."/";
// echo $dir;exit();
if (!is_dir($dir)) {
mkdir(iconv("UTF-8", "GBK", $dir),0777,true);
} //路径不存在 创建路径
$upfile = $_FILES['upfile'];

$types = array("png", "jpg", "webp", "jpeg", "gif"); //声明支持的文件类型
for ($i = 0; $i < count($upfile['name']); $i++){
$name = $upfile['name'][$i];
$end = explode(".", $name);
$type = strtolower(end($end)); //在循环中取得每次要上传的文件类型
if (!in_array($type, $types)) {
$retmsg[] = array('status'=>1,'message'=>"第" . ($i + 1) . "个文件类型错误<br/>");
} else {
$error = $upfile['error'][$i]; //在循环中取得每次要上传的文件的错误情况
if ($error != 0) {
$retmsg[] = array('status'=>2,'message'=>"第" . ($i + 1) . "个文件上传错误<br/>");
} else {
$tmp_name = $upfile['tmp_name'][$i];//在循环中取得每次要上传的文件的临时文件
if (!is_uploaded_file($tmp_name)) {
$retmsg[] = array('status'=>3,'message'=>"第" . ($i + 1) . "个文件临时文件错误<br/>");
} else {
$newname = $dir.date("YmdHis") . rand(1, 10000) . "." . $type;
// $img_array[$i] = substr($newname,strpos($newname,'/skin'));
$data = file_get_contents($tmp_name);
$checkma = md5(base64_encode($data));

$res = $empire->fetch1("select * from {$dbtbpre}upload_files where checkma='".$checkma."'");
if($res){
$retmsg[] = array('status'=>5,'message'=>"第" . ($i + 1) . "个文件上传失败,文件重复!<br/>");
}else{
//在循环中给每个文件一个新名称
if (!move_uploaded_file($tmp_name, $newname)) { //对每个临时文件执行上传操作
$retmsg[] = array('status'=>4,'message'=>"第" . ($i + 1) . "个文件上传失败<br/>");
} else {
$retmsg[] = array('status'=>0,'message'=>"第" . ($i + 1) . "个文件上传成功<br/>",'checkma'=>$checkma,'name'=>$name,'imgpath'=>'https://'.$_SERVER['HTTP_HOST'].substr($newname,strpos($newname,'/ziyuan')));
}
}
}
}
}
}
}
return $retmsg;
}

posted @ 2021-11-30 10:06  宋先生日记  阅读(367)  评论(0编辑  收藏  举报