首页 技术 正文
技术 2022年11月14日
0 收藏 722 点赞 3,042 浏览 4938 个字
 http://www.resumablejs.com/ 官网
upload.html
<!DOCTYPE html>
<html lang="en"> <div>
<a href="#" rel="external nofollow" id="browseButton" >Select files</a>
<div>
<div>
<input id="btnCancel" type="button" onClick='r.pause()'value="Cancel All Uploads"
style="margin-left: 2px; height: 22px; font-size: 8pt;" />
<br />
</div>
<script src="resumable.js"></script>
<script>
var r = new Resumable({
target:'upload.php',
chunkSize:**,
simultaneousUploads:,
testChunks:true,
throttleProgressCallbacks:, }); r.assignBrowse(document.getElementById('browseButton')); r.on('fileSuccess', function(file){
// console.debug(file);
});
r.on('fileProgress', function(file){
// console.debug(file);
});
r.on('fileAdded', function(file, event){
r.upload();
//console.debug(file, event);
});
r.on('fileRetry', function(file){
//console.debug(file);
});
r.on('fileError', function(file, message){
//console.debug(file, message);
});
r.on('uploadStart', function(){
//console.debug();
});
r.on('complete', function(){
//console.debug();
});
r.on('progress', function(){
//console.debug();
});
r.on('error', function(message, file){
//console.debug(message, file);
});
r.on('pause', function(file,message){
//console.debug(); });
r.on('cancel', function(){
//console.debug();
});
</script> </html> upload.php
<?php
/**
* This is the implementation of the server side part of
* Resumable.js client script, which sends/uploads files
* to a server in several chunks.
*
* The script receives the files in a standard way as if
* the files were uploaded using standard HTML form (multipart).
*
* This PHP script stores all the chunks of a file in a temporary
* directory (`temp`) with the extension `_part<#ChunkN>`. Once all
* the parts have been uploaded, a final destination file is
* being created from all the stored parts (appending one by one).
*
* @author Gregory Chris (http://online-php.com)
* @email www.online.php@gmail.com
*/ ////////////////////////////////////////////////////////////////////
// THE FUNCTIONS
//////////////////////////////////////////////////////////////////// /**
*
* Logging operation - to a file (upload_log.txt) and to the stdout
* @param string $str - the logging string
*/
function _log($str) { // log to the output
$log_str = date('d.m.Y').": {$str}\r\n";
echo $log_str; // log to file
if (($fp = fopen('upload_log.txt', 'a+')) !== false) {
fputs($fp, $log_str);
fclose($fp);
}
} /**
*
* Delete a directory RECURSIVELY
* @param string $dir - directory path
* @link http://php.net/manual/en/function.rmdir.php
*/
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir . "/" . $object) == "dir") {
rrmdir($dir . "/" . $object);
} else {
unlink($dir . "/" . $object);
}
}
}
reset($objects);
rmdir($dir);
}
} /**
*
* Check if all the parts exist, and
* gather all the parts of the file together
* @param string $dir - the temporary directory holding all the parts of the file
* @param string $fileName - the original file name
* @param string $chunkSize - each chunk size (in bytes)
* @param string $totalSize - original file size (in bytes)
*/
function createFileFromChunks($temp_dir, $fileName, $chunkSize, $totalSize) { // count all the parts of this file
$total_files = ;
foreach(scandir($temp_dir) as $file) {
if (stripos($file, $fileName) !== false) {
$total_files++;
}
} // check that all the parts are present
// the size of the last part is between chunkSize and 2*$chunkSize
if ($total_files * $chunkSize >= ($totalSize - $chunkSize + )) { // create the final destination file
if (($fp = fopen('temp/'.$fileName, 'w')) !== false) {
for ($i=; $i<=$total_files; $i++) {
fwrite($fp, file_get_contents($temp_dir.'/'.$fileName.'.part'.$i));
_log('writing chunk '.$i);
}
fclose($fp);
} else {
_log('cannot create the destination file');
return false;
} // rename the temporary directory (to avoid access from other
// concurrent chunks uploads) and than delete it
if (rename($temp_dir, $temp_dir.'_UNUSED')) {
rrmdir($temp_dir.'_UNUSED');
} else {
rrmdir($temp_dir);
}
} } ////////////////////////////////////////////////////////////////////
// THE SCRIPT
//////////////////////////////////////////////////////////////////// //check if request is GET and the requested chunk exists or not. this makes testChunks work
if ($_SERVER['REQUEST_METHOD'] === 'GET') { $temp_dir = 'temp/'.$_GET['resumableIdentifier'];
$chunk_file = $temp_dir.'/'.$_GET['resumableFilename'].'.part'.$_GET['resumableChunkNumber'];
if (file_exists($chunk_file)) {
header("HTTP/1.0 200 Ok");
} else
{
header("HTTP/1.0 404 Not Found");
}
} // loop through files and move the chunks to a temporarily created directory
if (!empty($_FILES)) foreach ($_FILES as $file) { // check the error status
if ($file['error'] != ) {
_log('error '.$file['error'].' in file '.$_POST['resumableFilename']);
continue;
} // init the destination file (format <filename.ext>.part<#chunk>
// the file is stored in a temporary directory
$temp_dir = 'temp/'.$_POST['resumableIdentifier'];
$dest_file = $temp_dir.'/'.$_POST['resumableFilename'].'.part'.$_POST['resumableChunkNumber']; // create the temporary directory
if (!is_dir($temp_dir)) {
mkdir($temp_dir, , true);
} // move the temporary file
if (!move_uploaded_file($file['tmp_name'], $dest_file)) {
_log('Error saving (move_uploaded_file) chunk '.$_POST['resumableChunkNumber'].' for file '.$_POST['resumableFilename']);
} else { // check if all the parts present, and create the final destination file
createFileFromChunks($temp_dir, $_POST['resumableFilename'],
$_POST['resumableChunkSize'], $_POST['resumableTotalSize']);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,985
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,501
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,345
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,128
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,763
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,839