1){ $index+=1; } $size = isset($_POST['size']) ? $_POST['size'] : null; //文件大小 $path = isset($_POST['path']) ? $_POST['path'] : ''; //文件路径 if(!$file || !$name){ jsonMsg(0,'没有文件'); } $info = pathinfo($name); //文件信息 $ext = isset($info['extension'])?$info['extension']:''; // 简单的判断文件类型 $imgarr = array('php','exe'); //不允许的格式 if(in_array($ext,$imgarr)){ jsonMsg(0,'文件类型出错'); } date_default_timezone_set('PRC'); //北京时间 $iscp = $path != ''; //是否自定义路径 if($iscp){ $path = rtrim($_SERVER['DOCUMENT_ROOT'].'/'.ltrim($path,'/'),'/').'/'; //上传文件夹时,追加文件夹路径 if($directorypath != ''){ $path = $path.str_replace($name,'',$directorypath); } }else{ $path = $_SERVER['DOCUMENT_ROOT'].'/'.date("Y/m/d",time()).'/'; } //判断目录存在否 if (!is_dir($path)){ mkdir($path,0777,true); } //新文件名 if($iscp){ $newname = basename($name); }else{ if($total>1){ $newname = $newname.'.'.$ext; }else{ $newname = date("His",time()).rand(1000, 9999).'.'.$ext; } } $newfile = $path.$newname; //新文件完整路径 $url = str_replace($_SERVER['DOCUMENT_ROOT'],"",$newfile); // 文件可访问的地址 // 这里判断有没有上传的文件流 if ($file['error'] == 0) { // 如果文件不存在,就创建 if (!file_exists($newfile)) { if (!move_uploaded_file($file['tmp_name'], $newfile)) { jsonMsg(0,'无法移动文件'); } // 片数相等,等于完成了 if($index == $total ){ jsonMsg(2,'上传完成',$url,$newname); } jsonMsg(1,'正在上传'); }else{ if($index == 1){ unlink($newfile); } } // 如果当前片数小于等于总片数,就在文件后继续添加 if($index <= $total){ $content = file_get_contents($file['tmp_name']); if (!file_put_contents($newfile, $content, FILE_APPEND)) { jsonMsg(0,'无法写入文件'); } // 片数相等,等于完成了 if($index == $total ){ jsonMsg(2,'上传完成',$url,$newname); } jsonMsg(1,'正在上传'); } } else { jsonMsg(0,'没有上传文件'); } // 输出json信息 function jsonMsg($code,$msg,$url='',$newname=''){ $arr['code'] = $code; $arr['msg'] = $msg; $arr['url'] = $url; $arr['name'] = $newname; echo json_encode($arr); die(); }