<?php // make sure file is not cached (as it happens for example on ios devices) header("expires: mon, 26 jul 1997 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"); if ($_server['request_method'] == 'options') { exit; // finish preflight cors requests here } if ( !empty($_request[ 'debug' ]) ) { $random = rand(0, intval($_request[ 'debug' ]) ); if ( $random === 0 ) { header("http/1.0 500 internal server error"); exit; } } // header("http/1.0 500 internal server error"); // exit; @set_time_limit(5 * 60); // uncomment this one to fake upload time // usleep(5000); // settings // $targetdir = ini_get("upload_tmp_dir") . directory_separator . "plupload"; $targetdir = 'upload_tmp'; $uploaddir = 'upload'; $cleanuptargetdir = true; $maxfileage = 5 * 3600; if (!file_exists($targetdir)) { @mkdir($targetdir); } if (!file_exists($uploaddir)) { @mkdir($uploaddir); } if (isset($request["name"])) { $filename = $_request["name"]; } elseif (!empty($_files)) { $filename = $_files["file"]["name"]; } else { $filename = uniqid("file"); } //------------------------------------------------------- //无论是什么文件名称,先unicode转utf8 //unicode转utf8 //注意$str='"..."',内部双引号,外部单引号 //对于变量$str='..',我们需要处理'"'.$str.'"',处理后成一个新变量 function unicode2utf8($str){ if(!$str) return $str; $decode = json_decode($str); if($decode) return $decode; $str = '["' . $str . '"]'; $decode = json_decode($str); if(count($decode) == 1){ return $decode[0]; } return $str; } $filename=unicode2utf8('"'.$filename.'"'); $filename= iconv("utf-8", "gbk", $filename);//防止fopen语句失效 //----------------------------------------------------------------------- $filepath = $targetdir . directory_separator . $filename; $uploadpath = $uploaddir . directory_separator . $filename; // chunking might be enabled $chunk = isset($_request["chunk"]) ? intval($_request["chunk"]) : 0; $chunks = isset($_request["chunks"]) ? intval($_request["chunks"]) : 1; // remove old temp files if ($cleanuptargetdir) { if (!is_dir($targetdir) || !$dir = opendir($targetdir)) { die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "failed to open temp directory."}, "id" : "id"}'); while (($file = readdir($dir)) !== false) { $tmpfilepath = $targetdir . directory_separator . $file; // if temp file is current file proceed to the next if ($tmpfilepath == "{$filepath}_{$chunk}.part" || $tmpfilepath == "{$filepath}_{$chunk}.parttmp") { continue; } // remove temp file if it is older than the max age and is not the current file if (preg_match('/\.(part|parttmp)$/', $file) && (@filemtime($tmpfilepath) < time() - $maxfileage)) { @unlink($tmpfilepath); } }//while closedir($dir); } // open temp file if (!$out = @fopen("{$filepath}_{$chunk}.parttmp", "wb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "failed to open output stream."}, "id" : "id"}'); } if (!empty($_files)) { if ($_files["file"]["error"] || !is_uploaded_file($_files["file"]["tmp_name"])) { die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "failed to move uploaded file."}, "id" : "id"}'); } // 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": "failed to open input stream."}, "id" : "id"}'); } } else { if (!$in = @fopen("php://input", "rb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "failed to open input stream."}, "id" : "id"}'); } } while ($buff = fread($in, 4096)) { fwrite($out, $buff); } @fclose($out); @fclose($in); rename("{$filepath}{$chunk}.parttmp", "{$filepath}{$chunk}.part"); $index = 0; $done = true; for( $index = 0; $index < $chunks; $index++ ) { if ( !file_exists("{$filepath}{$index}.part") ) { $done = false; break; } } if ( $done ) { if (!$out = @fopen($uploadpath, "wb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "failed to open output stream."}, "id" : "id"}'); } //flock($hander,lock_ex)文件锁 if ( flock($out, lock_ex) ) { for( $index = 0; $index < $chunks; $index++ ) { if (!$in = @fopen("{$filepath}{$index}.part", "rb")) { break; } while ($buff = fread($in, 4096)) { fwrite($out, $buff); } @fclose($in); @unlink("{$filepath}_{$index}.part"); } flock($out, lock_un); } @fclose($out); //我们可以在这里进行入库操作,以json_encode(数组)返回给客户端,由uploadsuccess事件处理 } ?>
