/*** upload.php */
header("Expires: Mon, 26 Jul 2001 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
$chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 1;
$uploadDir = 'files';
$targetDir = 'files/temp' ;
// Support CORS
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
exit;
}
// 3 minutes execution time
@set_time_limit(3 * 60);
// Create target dir
if (!file_exists($uploadDir)) {
@mkdir($uploadDir);
}
if (!file_exists($targetDir)) {
@mkdir($uploadDir);
}
//
$uploadDir = $uploadDir .'/'. Date('Ym') ;
if (!file_exists($uploadDir)) {
@mkdir($uploadDir);
}
function gen_funame( $img){
$path_part = pathinfo( $img );
if( !$path_part["extension"]){
$path_part["extension"] = 'png';
}
if( $path_part["extension"] ==='jpeg'){
$path_part["extension"] = 'jpg';
}
if( class_exists('datetime') ){
$new_time = new DateTime( );
$g_time = $new_time->format('dHs') ;
}else{
$g_time = Date('dHs') ;
}
return $g_time.'_' . mt_rand( 1000 ,9999 ) .'.'.$path_part["extension"] ;
}
// Get a file name
if (isset($_REQUEST["name"])) {
$file_name = $_REQUEST["name"];
} elseif (!empty($_FILES)) {
$file_name = $_FILES["file"]["name"];
} else {
$file_name = uniqid("file_");
}
if( $chunks === 1 ){
$file_name = gen_funame( $file_name ) ;
}
$filePath = $targetDir .'/'. $file_name;
$uploadPath = $uploadDir .'/'. $file_name;
// Open temp file
if (!$out = @fopen("{$filePath}_{$chunk}.part", "wb")) {
die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "输出流打开失败"} }');
}
if (!empty($_FILES)) {
if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "上传文件打开失败"} }');
}
// Read binary input stream and append it to temp file
if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "输入流打开失败"} }');
}
} else {
if (!$in = @fopen("php://input", "rb")) {
die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "输入流打开失败"} }');
}
}
while ($buff = fread($in, 4096)) {
fwrite($out, $buff);
}
@fclose($out);
@fclose($in);
//rename("{$filePath}_{$chunk}.parttmp", "{$filePath}_{$chunk}.part");
$index = 0;
$done = true;
function write_stream( $out , $name){
//{$filePath}_{$index}.part
if (!$in = @fopen( $name , "rb")) {
break;
}
while ($buff = fread($in, 4096)) {
fwrite($out, $buff);
}
@fclose($in);
@unlink( $name );
}
if (!$out = @fopen($uploadPath, "wb")) {
die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "输出流打开失败"} }');
}
//if( $chunk && $chunks ){
for( $index = 0; $index < $chunks; $index++ ) {
if ( !file_exists("{$filePath}_{$index}.part") ) {
$done = false;
break;
}
}
if ( $done ) {
$name = '';
if ( flock($out, LOCK_EX) ) {
for( $index = 0; $index < $chunks; $index++ ) {
$name = $filePath .'_' .$index .'.part';
write_stream( $out , $name) ;
}
flock($out, LOCK_UN);
}
}
//}else{
//$name = $filePath.'_'.$index'.part';
//write_stream( $out , $name) ;
//}
@fclose($out);
die('{"jsonrpc" : "2.0", "file" : "'.$uploadPath .'"}');
一键复制
编辑
Web IDE
原始数据
按行查看
历史