<?php
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:PUT,POST,GET,DELETE,OPTIONS');
header('Access-Control-Allow-Headers:*');
header('Content-Type:application/json; charset=utf-8');
$file = isset($_FILES['file']) ? $_FILES['file']:null;
$name = isset($_POST['name']) ? $_POST['name']:null;
$newname = isset($_POST['newname']) ? $_POST['newname']:null;
$directorypath = isset($_POST['directorypath']) ? $_POST['directorypath']:'';
$total = isset($_POST['chunks']) ? $_POST['chunks']:0;
$index = isset($_POST['chunk']) ? $_POST['chunk']:0;
if($total>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,'没有上传文件');
}
function jsonMsg($code,$msg,$url='',$newname=''){
$arr['code'] = $code;
$arr['msg'] = $msg;
$arr['url'] = $url;
$arr['name'] = $newname;
echo json_encode($arr);
die();
}