我想使用PHP上传视频文件,并通过进度条显示上传进度。但是这样比较困难,就像我认为的那样,我试图把拼凑起来的拼图放在一起,但不幸的是我没有找到所需的php,ajax和html代码一起工作的代码片段,所以我试图将不同的拼凑在一起。使用Javascript和PHP的视频文件上传进度条
我的代码功能几乎完全。唯一的问题是,目前的文件上传过程中,我已经得到了百分之,是由我的JavaScript加载后,过程已经结束,而不是从一开始。
这是我的PHP代码:
function file_get_size($file) {
//open file
$fh = fopen($file, "r");
//declare some variables
$size = "0";
$char = "";
//set file pointer to 0; I'm a little bit paranoid, you can remove this
fseek($fh, 0, SEEK_SET);
//set multiplicator to zero
$count = 0;
while (true) {
//jump 1 MB forward in file
fseek($fh, 1048576, SEEK_CUR);
//check if we actually left the file
if (($char = fgetc($fh)) !== false) {
//if not, go on
$count ++;
} else {
//else jump back where we were before leaving and exit loop
fseek($fh, -1048576, SEEK_CUR);
break;
}
}
//we could make $count jumps, so the file is at least $count * 1.000001 MB large
//1048577 because we jump 1 MB and fgetc goes 1 B forward too
$size = bcmul("1048577", $count);
//now count the last few bytes; they're always less than 1048576 so it's quite fast
$fine = 0;
while(false !== ($char = fgetc($fh))) {
$fine ++;
}
//and add them
$size = bcadd($size, $fine);
fclose($fh);
return $size;
}
$filesize = file_get_size('remote-file');
$remote = fopen('remote-file', 'r');
$local = fopen('local-file', 'w');
$read_bytes = 0;
while(!feof($remote)) {
$buffer = fread($remote, 2048);
fwrite($local, $buffer);
$read_bytes += 2048;
//Use $filesize as calculated earlier to get the progress percentage
$progress = min(100, 100 * $read_bytes/$filesize);
fwrite(fopen('files/upload/progress.txt', 'w'), $progress);
//you'll need some way to send $progress to the browser.
//maybe save it to a file and then let an Ajax call check it?
}
fclose($remote);
fclose($local);
这是我的JavaScript代码:
function main()
{
var pathOfFileToRead = "files/upload/progress.txt";
var contentsOfFileAsString = FileHelper.readStringFromFileAtPath
(
pathOfFileToRead
);
document.body.innerHTML = contentsOfFileAsString;
}
function FileHelper()
{}
{
FileHelper.readStringFromFileAtPath = function(pathOfFileToReadFrom)
{
var request = new XMLHttpRequest();
request.open("GET", pathOfFileToReadFrom, false);
request.send(null);
var returnValue = request.responseText;
return returnValue;
}
}
main();
function progressBarSim(al) {
var bar = document.getElementById('bar-fill');
var status = document.getElementById('status');
status.innerHTML = al+"%";
bar.value = al;
al++;
var sim = setTimeout("progressBarSim("+al+")",1000);
if(al == 100){
status.innerHTML = "100%";
bar.value = 100;
clearTimeout(sim);
var finalMessage = document.getElementById('finalMessage');
finalMessage.innerHTML = "Process is complete";
}
}
var amountLoaded = 0;
progressBarSim(amountLoaded);
进度条就目前的工作在一个定时器,因为main()函数犯规阅读的内容“progress.txt”从一开始,但只在最后。所以我想有一些帮助将progressBarSim和main()结合起来。
* 编辑:*我发现了一段代码:http://www.it-gecko.de/html5-file-upload-fortschrittanzeige-progressbar.html,我现在正在使用它。
你真的明白ajax是什么吗? 它看起来像你没有,Ajax被用来调用一个PHP脚本,并从它获得响应,而无需在浏览器中重新加载页面。 所以你在这里使用的不是Ajax,它不能工作,因为PHP将首先完全执行。所以当浏览器使用主函数时,文件表示完成是合乎逻辑的。 – MisterJ
你也用ffmpeg? – cocco
我没有使用ffmpeg,也没有太多有关javascript和ajax的经验,这就是为什么我犯了这个错误。 – hardking