[Question] Get page percent loaded ...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fishown
Forum Commoner
Posts: 33
Joined: Sat May 12, 2007 5:35 pm

[Question] Get page percent loaded ...

Post 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 =].
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: [Question] Get page percent loaded ...

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: [Question] Get page percent loaded ...

Post 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>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply