问题描述
我们想创建一种文件上传服务。我们使用 resumable.js 和 PHP 来上传文件。这部分工作正常(它上传,制作一个文件,然后将文件放在上传文件夹中)。
但是我们想在上传后将文件重命名为随机名称(这也不是问题),但问题是,如何将随机文件名返回给用户?我没有看到将一些文本/数据返回给用户的(好的)方法?
有人可以提供帮助/建议吗?
<?PHP
/**
*
* 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 $temp_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,$total_files) {
// count all the parts of this file
$total_files_on_server_size = 0;
$temp_total = 0;
foreach(scandir($temp_dir) as $file) {
$temp_total = $total_files_on_server_size;
$tempfilesize = filesize($temp_dir.'/'.$file);
$total_files_on_server_size = $temp_total + $tempfilesize;
}
// check that all the parts are present
// If the Size of all the chunks on the server is equal to the size of the file uploaded.
if ($total_files_on_server_size >= $totalSize) {
// create the final destination file
if (($fp = fopen($temp_dir.'/'.$fileName,'w')) !== false) {
for ($i=1; $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')) {
//Here is the moving to uploads folder done:
rename($temp_dir.'_UNUSED'.'/'.$fileName,'uploads/'.$fileName);
rrmdir($temp_dir.'_UNUSED');
} else {
rrmdir($temp_dir);
}
}
}
//check if request is GET and the requested chunk exists or not. this makes testChunks work
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if(!(isset($_GET['resumableIdentifier']) && trim($_GET['resumableIdentifier'])!='')){
$_GET['resumableIdentifier']='';
}
$temp_dir = 'temp/'.$_GET['resumableIdentifier'];
if(!(isset($_GET['resumableFilename']) && trim($_GET['resumableFilename'])!='')){
$_GET['resumableFilename']='';
}
if(!(isset($_GET['resumableChunkNumber']) && trim($_GET['resumableChunkNumber'])!='')){
$_GET['resumableChunkNumber']='';
}
$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'] != 0) {
_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
if(isset($_POST['resumableIdentifier']) && trim($_POST['resumableIdentifier'])!=''){
$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,0777,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'],$_POST['resumabletotalChunks']);
}
}
uploads.js:
(function () {
var r = new Resumable({
target: '/beta/upload.PHP',query: {upload_token:'token'},maxChunkRetries: 2,maxFiles: 3,prioritizefirstAndLastChunk: true,simultaneousuploads: 4,chunkSize: 1 * 1024 * 1024
});
var results = $('#results'),draggable = $('#dragHere'),uploadFile = $('#uploadFiles'),browseButton = $('#browseButton'),nothingToUpload = $('[data-nothingToUpload]');
// if resumable is not supported aka IE
if (!r.support) location.href = 'http://browsehappy.com/';
r.assignbrowse(browseButton);
r.assignDrop(draggable);
r.on('fileAdded',function (file,event) {
var template =
'<div data-uniqueid="' + file.uniqueIdentifier + '">' +
'<div class="fileName">' + file.fileName + ' (' + file.file.type + ')' + '</div>' +
'<div class="large-6 right deleteFile">X</div>' +
'<div class="progress large-6">' +
'<span class="meter" style="width:0%;"></span>' +
'</div>' +
'</div>';
results.append(template);
});
uploadFile.on('click',function () {
if (results.children().length > 0) {
r.upload();
} else {
nothingToUpload.fadeIn();
setTimeout(function () {
nothingToUpload.fadeOut();
},3000);
}
});
$(document).on('click','.deleteFile',function () {
var self = $(this),parent = self.parent(),identifier = parent.data('uniqueid'),file = r.getFromUniqueIdentifier(identifier);
r.removeFile(file);
parent.remove();
});
r.on('fileProgress',function (file) {
var progress = Math.floor(file.progress() * 100);
$('[data-uniqueId=' + file.uniqueIdentifier + ']').find('.meter').css('width',progress + '%');
$('[data-uniqueId=' + file.uniqueIdentifier + ']').find('.meter').html(' ' + progress + '%');
});
r.on('fileSuccess',message) {
$('[data-uniqueId=' + file.uniqueIdentifier + ']').find('.progress').addClass('success');
});
r.on('uploadStart',function () {
$('.alert-Box').text('Uploading....');
});
r.on('complete',function () {
$('.alert-Box').text("Done");
});
})();
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)