php结合web uploader插件实现分片上传文件
使用
使用webuploader分成简单直选要引入
HTML部分
uploader = WebUploader.create({
// 不压缩image
resize: false,
// swf文件路径
swf: ‘uploader.swf’,
// 文件接收服务端。
server: upload.php,
// 选择文件的按钮。可选。
// 内部根据当前运行是创建,可能是input元素,也可能是flash.
pick: ‘#picker’,
chunked: true,
chunkSize:210241024,
auto: true,
accept: {
title: ‘Images’,
extensions: ‘gif,jpg,jpeg,bmp,png’,
mimeTypes: ‘image/*’
}
});
upload.php处理
以下是根据别人的例子自己拿来改的php 后台代码
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 (
S
E
R
V
E
R
[
′
R
E
Q
U
E
S
T
M
E
T
H
O
D
′
]
=
=
′
O
P
T
I
O
N
S
′
)
e
x
i
t
;
/
/
f
i
n
i
s
h
p
r
e
f
l
i
g
h
t
C
O
R
S
r
e
q
u
e
s
t
s
h
e
r
e
i
f
(
!
e
m
p
t
y
(
_SERVER['REQUEST_METHOD'] == 'OPTIONS') { exit; // finish preflight CORS requests here } if ( !empty(
SERVER[′REQUESTMETHOD′]==′OPTIONS′)exit;//finishpreflightCORSrequestshereif(!empty(_REQUEST[ ‘debug’ ]) ) {
r
a
n
d
o
m
=
r
a
n
d
(
0
,
i
n
t
v
a
l
(
random = rand(0, intval(
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;
// 5 minutes execution time
@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 = ‘uploads’.DIRECTORY_SEPARATOR.‘file_material_tmp’;
$uploadDir = ‘uploads’.DIRECTORY_SEPARATOR.‘file_material’;
$cleanupTargetDir = true; // Remove old files
m
a
x
F
i
l
e
A
g
e
=
5
∗
3600
;
/
/
T
e
m
p
f
i
l
e
a
g
e
i
n
s
e
c
o
n
d
s
/
/
C
r
e
a
t
e
t
a
r
g
e
t
d
i
r
i
f
(
!
f
i
l
e
e
x
i
s
t
s
(
maxFileAge = 5 * 3600; // Temp file age in seconds // Create target dir if (!file_exists(
maxFileAge=5∗3600;//Tempfileageinseconds//Createtargetdirif(!fileexists(targetDir)) {
@mkdir(KaTeX parse error: Expected 'EOF', got '}' at position 15: targetDir); }̲ // Create ta…uploadDir)) {
@mkdir(KaTeX parse error: Expected 'EOF', got '}' at position 15: uploadDir); }̲ // Get a fil…_REQUEST[“name”])) {
$fileName = KaTeX parse error: Expected 'EOF', got '}' at position 21: …EST["name"]; }̲ elseif (!empty…_FILES)) {
$fileName = $FILES[“file”][“name”];
} else {
$fileName = uniqid("file");
}
$oldName = $fileName;
$filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
// $uploadPath = $uploadDir . DIRECTORY_SEPARATOR . $fileName;
// Chunking might be enabled
c
h
u
n
k
=
i
s
s
e
t
(
chunk = isset(
chunk=isset(_REQUEST[“chunk”]) ? intval($_REQUEST[“chunk”]) : 0;
c
h
u
n
k
s
=
i
s
s
e
t
(
chunks = isset(
chunks=isset(REQUEST[“chunks”]) ? intval(
R
E
Q
U
E
S
T
[
"
c
h
u
n
k
s
"
]
)
:
1
;
/
/
R
e
m
o
v
e
o
l
d
t
e
m
p
f
i
l
e
s
i
f
(
_REQUEST["chunks"]) : 1; // Remove old temp files if (
REQUEST["chunks"]):1;//Removeoldtempfilesif(cleanupTargetDir) {
if (!is_dir(
t
a
r
g
e
t
D
i
r
)
∣
∣
!
targetDir) || !
targetDir)∣∣!dir = opendir(
t
a
r
g
e
t
D
i
r
)
)
d
i
e
(
′
"
j
s
o
n
r
p
c
"
:
"
2.0
"
,
"
e
r
r
o
r
"
:
"
c
o
d
e
"
:
100
,
"
m
e
s
s
a
g
e
"
:
"
F
a
i
l
e
d
t
o
o
p
e
n
t
e
m
p
d
i
r
e
c
t
o
r
y
.
"
,
"
i
d
"
:
"
i
d
"
′
)
;
w
h
i
l
e
(
(
targetDir)) { die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}'); } while ((
targetDir))die(′"jsonrpc":"2.0","error":"code":100,"message":"Failedtoopentempdirectory.","id":"id"′);while((file = readdir($dir)) !== false) {
$tmpfilePath = $targetDir . DIRECTORY_SEPARATOR .
f
i
l
e
;
/
/
I
f
t
e
m
p
f
i
l
e
i
s
c
u
r
r
e
n
t
f
i
l
e
p
r
o
c
e
e
d
t
o
t
h
e
n
e
x
t
i
f
(
file; // If temp file is current file proceed to the next if (
file;//Iftempfileiscurrentfileproceedtothenextif(tmpfilePath == “{KaTeX parse error: Expected 'EOF', got '}' at position 9: filePath}̲_{chunk}.part” || KaTeX parse error: Expected '}', got 'EOF' at end of input: …pfilePath == "{filePath}{KaTeX parse error: Expected 'EOF', got '}' at position 6: chunk}̲.parttmp") { …/’, KaTeX parse error: Expected 'EOF', got '&' at position 7: file) &̲& (@filemtime(tmpfilePath) < time() - KaTeX parse error: Expected '}', got 'EOF' at end of input: …{ @unlink(tmpfilePath);
}
}
closedir($dir);
}
// Open temp file
if (!KaTeX parse error: Expected '}', got 'EOF' at end of input: out = @fopen("{filePath}_{KaTeX parse error: Expected 'EOF', got '}' at position 6: chunk}̲.parttmp", "wb"…FILES)) {
if (
F
I
L
E
S
[
"
f
i
l
e
"
]
[
"
e
r
r
o
r
"
]
∣
∣
!
i
s
u
p
l
o
a
d
e
d
f
i
l
e
(
_FILES["file"]["error"] || !is_uploaded_file(
FILES["file"]["error"]∣∣!isuploadedfile(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 (!
i
n
=
@
f
o
p
e
n
(
in = @fopen(
in=@fopen(FILES[“file”][“tmp_name”], “rb”)) {
die(’{“jsonrpc” : “2.0”, “error” : {“code”: 101, “message”: “Failed to open input stream.”}, “id” : “id”}’);
}
} else {
if (!KaTeX parse error: Expected 'EOF', got '}' at position 157: …id"}'); } }̲ while (buff = fread(KaTeX parse error: Expected '}', got 'EOF' at end of input: …)) { fwrite(out, KaTeX parse error: Expected 'EOF', got '}' at position 10: buff); }̲ @fclose(out);
@fclose(KaTeX parse error: Expected '}', got 'EOF' at end of input: …n); rename("{filePath}{KaTeX parse error: Expected 'EOF', got '}' at position 6: chunk}̲.parttmp", "{filePath}{$chunk}.part");
$index = 0;
$done = true;
for( $index = 0; $index < $chunks; KaTeX parse error: Expected '}', got 'EOF' at end of input: …!file_exists("{filePath}{$index}.part") ) {
$done = false;
break;
}
}
if ( $done ) {
p
a
t
h
I
n
f
o
=
p
a
t
h
i
n
f
o
(
pathInfo = pathinfo(
pathInfo=pathinfo(fileName);
h
a
s
h
S
t
r
=
s
u
b
s
t
r
(
m
d
5
(
hashStr = substr(md5(
hashStr=substr(md5(pathInfo[‘basename’]),8,16);
$hashName = time() .
h
a
s
h
S
t
r
.
′
.
′
.
hashStr . '.' .
hashStr.′.′.pathInfo[‘extension’];
$uploadPath =
u
p
l
o
a
d
D
i
r
.
D
I
R
E
C
T
O
R
Y
S
E
P
A
R
A
T
O
R
.
uploadDir . DIRECTORY_SEPARATOR .
uploadDir.DIRECTORYSEPARATOR.hashName;
if (!
o
u
t
=
@
f
o
p
e
n
(
out = @fopen(
out=@fopen(uploadPath, “wb”)) {
die(’{“jsonrpc” : “2.0”, “error” : {“code”: 102, “message”: “Failed to open output stream.”}, “id” : “id”}’);
}
if ( flock($out, LOCK_EX) ) {
for( $index = 0; $index < $chunks; KaTeX parse error: Expected '}', got 'EOF' at end of input: … ) { if (!in = @fopen("{KaTeX parse error: Expected 'EOF', got '}' at position 9: filePath}̲_{index}.part", “rb”)) {
break;
}
while (
b
u
f
f
=
f
r
e
a
d
(
buff = fread(
buff=fread(in, 4096)) {
fwrite($out, KaTeX parse error: Expected 'EOF', got '}' at position 13: buff); }̲ @fclose(in);
@unlink("{KaTeX parse error: Expected 'EOF', got '}' at position 9: filePath}̲_{index}.part");
}
flock(KaTeX parse error: Expected 'EOF', got '}' at position 19: …, LOCK_UN); }̲ @fclose(out);
r
e
s
p
o
n
s
e
=
[
′
s
u
c
c
e
s
s
′
=
>
t
r
u
e
,
′
o
l
d
N
a
m
e
′
=
>
response = [ 'success'=>true, 'oldName'=>
response=[′success′=>true,′oldName′=>oldName,
‘filePaht’=>
u
p
l
o
a
d
P
a
t
h
,
′
f
i
l
e
S
i
z
e
′
=
>
uploadPath, 'fileSize'=>
uploadPath,′fileSize′=>data[‘size’],
‘fileSuffixes’=>
p
a
t
h
I
n
f
o
[
′
e
x
t
e
n
s
i
o
n
′
]
,
′
f
i
l
e
i
d
′
=
>
pathInfo['extension'], 'file_id'=>
pathInfo[′extension′],′fileid′=>data[‘id’],
];
die(json_encode($response));
}
// Return Success JSON-RPC response
die(’{“jsonrpc” : “2.0”, “result” : null, “id” : “id”}’);