php 百度上传分片,上传文件建议增加分片上传功能

有相关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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值