Page 1 of 1
[Question] Get page percent loaded ...
Posted: Tue Feb 01, 2011 5:11 pm
by fishown
Just wonder,
Is ther any way to to read how much percent of the current page is loaded? or how much left?
Im transfering file thrught ftps, and i want to show an progress bar.
thanks =].
Re: [Question] Get page percent loaded ...
Posted: Tue Feb 01, 2011 10:58 pm
by s.dot
You would need to
1) Get the size of the full file
2) Get how much of the file you have sent so far. With ftp_put, maybe you could loop and send X bytes at a time using the startpos parameter? I'm not sure of how to do this exactly.
3) Use javascript to update the page (or get fancy and make an image progress bar). The percent would be sent bytes / total bytes * 100.
Re: [Question] Get page percent loaded ...
Posted: Wed Feb 02, 2011 1:02 am
by s.dot
I was actually working on a proof of concept script, to read part of a file, write it to a local file, ftp it to the server (resuming the previous part sent), then deleting the local file.
This way bytes sent could be updated via javascript and a percent loaded signal could be shown.
I'm having trouble getting the upload to RESUME instead of start over, though.
Here's what I got.. I'm getting a headache. Feel free to play with it if you wish!
Code: Select all
<html>
<head>
<title>FTP Progress Script</title>
</head>
<body>
<!-- This div will show the progress -->
<div id="progress"></div>
<?php
ob_start();
ob_implicit_flush();
echo str_repeat(' ', 5024);
echo '<br />';
ob_flush();
//ftp details
$ftpServer = 'ftp.example.com';
$ftpPort = 21;
$ftpUser = 'username';
$ftpPass = 'password';
//local file name, remote file name, file transfer mode, file size, byte step
$ftpTransferFileName = 'E:\Torrents\House.S07E10.HDTV.XviD-LOL.avi';
$ftpTransferFileNameRemote = 'myupload2.avi';
$ftpTransferMode = FTP_BINARY;
$ftpTransferFileSize = filesize($ftpTransferFileName);
$ftpTransferByteStep = 256 * 1024; //256 kilobyte step
//connect to ftp
$ftpCon = ftp_connect($ftpServer, $ftpPort)
or die('Could not connect to FTP server (' . $ftpServer . ')');
//login
@ftp_login($ftpCon, $ftpUser, $ftpPass)
or die('Could not log in to FTP server.');
ftp_pasv($ftpCon, true);
//stop max_execution_time from being reached
set_time_limit(0);
//loop
$byteStart = 0;
$it=0;
while (true)
{
if (($byteStart + $ftpTransferByteStep) >= $ftpTransferFileSize)
{
echo 'reached max limit';
$lastStep = true;
}
//get part of file
$filePart = file_get_contents($ftpTransferFileName, false, null, $byteStart, $ftpTransferByteStep);
//write it to a file
file_put_contents('ftp.part.' . $it, $filePart);
//upload part
ftp_set_option($ftpCon, FTP_AUTOSEEK, true);
$size = ftp_size($ftpCon, $ftpTransferFileNameRemote);
if (ftp_put($ftpCon, $ftpTransferFileNameRemote, 'ftp.part.' . $it, $ftpTransferMode, $size))
{
echo $byteStart . ' bytes sent<br />';
ob_flush();
} else
{
die('part not sent');
}
$byteStart = $byteStart + $ftpTransferByteStep;
//remove local part file
unlink('ftp.part.' . $it);
if (isset($lastStep) && $lastStep)
{
break;
}
$it++;
}
die('connected');
?></body>
</html>