有相关demo代码吗,查看一下
前端可以用webuploader类支持切片传输实现,百度编辑器使用的是该插件,改造起立也方便。
后端部分可以看下我以前项目用到的部分代码:
public function upload($path, $name, $cover=false)
{
// HTTP headers for no cache etc
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");
// Look for the content type header
if (isset($_SERVER['HTTP_CONTENT_TYPE'])) $contentType = $_SERVER['HTTP_CONTENT_TYPE'];
if (isset($_SERVER['CONTENT_TYPE'])) $contentType = $_SERVER['CONTENT_TYPE'];
// Get parameters
$chunk = wf_gpc('chunk', 'r','intval');
$chunks = wf_gpc('chunks','r','intval');
// 处理文件名
$file = $this->get_gpath($path.$name);
if(!is_writeable($this->get_gpath($path))){
$this->error = '文件系统错误,当前目录没有写入权限';
return false;
} else if(!$cover && file_exists($file)){
$this->error = '文件系统错误,目标文件已存在';
return false;
} else if($cover && file_exists($file) && !unlink($file)){
$this->error = '文件系统错误,无法删除原始文件';
return false;
}
// 上传写文件步骤,这一部分以下的代码可直接引用
// Handle non multipart uploads older WebKit versions didn't support multipart in HTML5
if (strpos($contentType, 'multipart') !== false){
if (!isset($_FILES['file']['tmp_name']) || !is_uploaded_file($_FILES['file']['tmp_name'])) {
$this->error = 'Failed to move uploaded file.';
return false;
}
// 分块一直接move,减小不分块时服务器负载
if (0 == $chunk) {
if (!move_uploaded_file($_FILES['file']['tmp_name'], "{$file}.part")){
$tihs->error = 'Failed to open output stream.';
return false;
}
} else {
// 合并剩余分块数据
$out = fopen("{$file}.part", $chunk == 0 ? 'wb' : 'ab');
if (!$out) {
$tihs->error = 'Failed to open output stream.';
return false;
}
$in = fopen($_FILES['file']['tmp_name'], 'rb');
if (!$in) {
$tihs->error = 'Failed to open input stream.';
return false;
}
while ($buff = fread($in, 4096)) {
fwrite($out, $buff);
}
fclose($in);
fclose($out);
}
file_exists($_FILES['file']['tmp_name']) && unlink($_FILES['file']['tmp_name']);
} else {
$out = fopen("{$file}.part", $chunk == 0 ? 'wb' : 'ab');
if (!$out){
$tihs->error = 'Failed to open output stream.';
return false;
}
// Read binary input stream and append it to temp file
$in = fopen("php://input", 'rb');
if (!$in){
$tihs->error = 'Failed to open input stream.';
return false;
}
while ($buff = fread($in, 4096)){
fwrite($out, $buff);
}
fclose($in);
fclose($out);
}
// Check if file has been uploaded
if (!$chunks || $chunk == $chunks - 1) {
return rename("{$file}.part", $file);
}
return true;
}