Page 1 of 1
how to get the download complete time ?
Posted: Wed Aug 17, 2011 6:45 am
by uniojnqoifazy
I want to find complete download time in PHP . If I click download href , then i can get the time when the file have download complete . because i want to compute the download speed from server to client , Can anyone please help me out? Today is my deadline. Please check out my code:
Re: how to get the download complete time ?
Posted: Wed Aug 17, 2011 10:49 am
by phphelpme
You have not shown us any code?
Best wishes
Re: how to get the download complete time ?
Posted: Wed Aug 17, 2011 4:27 pm
by Dorin85
This concept should work. I wrote this for you but I did not test it.
Code: Select all
<?php
// This file is "randomname.php".
session_start();
$_SESSION['download_attempt'] = array('file_name' => 'randomname', 'time'=> array('start_time'=>time(),'end_time'=>null), 'status'=> 'started');
print file_get_contents("randomname.mpeg");
if (!connection_aborted()){
$_SESSION['download_attempt']['status'] = 'completed';
$_SESSION['download_attempt']['time']['end_time'] = time();
}
?>
link to the file:
<a href="randomname.php">filename</a>
And to check to see download status:
<?php
session_start();
if (isset($_SESSION['download_attempt']['time']['end_time'])){
//case: fully downloaded
$times = $_SESSION['download_attempt']['time'];
$download_time = $times['end_time'] - $times['start_time'];
}
?>
Alternatively you could use microtime() rather than time() to get some extra accuracy but you'll have to do something like...
Code: Select all
$time = explode(' ',microtime());
$time = (float)$time[0] + (float)$time[1];
...in order to get the time in microseconds.
Re: how to get the download complete time ?
Posted: Wed Aug 17, 2011 8:50 pm
by uniojnqoifazy
hi all,
Download.php
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php download</title>
</head>
<?
$filepath = './TestFiles/';
$filename = 'test.zip';
?>
<body>
<a href="get_file.php?path=<?=$filepath?>&filename=<?=urlencode($filename)?>"><?=$filename?></a>
</body>
</html>
get_file.php
Code: Select all
<?php
if(empty($_GET['path']) || empty($_GET['filename']))
{
echo 'error path';
exit();
}
if(file_exists($_GET['path']))
{
$FILEname = urlencode($_GET['filename']);
$file = $_GET['path'].'/'.$FILEname;
//Record the start time when user request to download the file ???
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header("Content-Type: application/octetstream; name=$FILEname"); //for IE & Opera
header("Content-Type: application/octet-stream; name=$FILEname"); //for the rest
header("Content-Disposition: attachment; filename=$FILEname;");
header("Content-Transfer-Encoding: binary");
header("Cache-Control: cache, must-revalidate");
header("Pragma: public");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
readfile($file);
//how can i get the end time that when user have fully download ???????
}
else
echo 'file doesnt exist';
?>
// compute the download time = end time - start time ??????
Re: how to get the download complete time ?
Posted: Wed Aug 17, 2011 9:00 pm
by uniojnqoifazy
hi Dorin85 ,
i have add your code into my code, but it's can't download , am i missing anything ?
Code: Select all
<?php
if(empty($_GET['path']) || empty($_GET['filename']))
{
echo 'error path';
exit();
}
if(file_exists($_GET['path']))
{
$FILEname = urlencode($_GET['filename']);
$file = $_GET['path'].'/'.$FILEname;
//I want to record the start time when user request to download the file ???
session_start();
$_SESSION['download_attempt'] = array('file_name' => $FILEname, 'time'=> array('start_time'=>time(),'end_time'=>null), 'status'=> 'started');
print file_get_contents($file);
if (!connection_aborted()){
$_SESSION['download_attempt']['status'] = 'completed';
$_SESSION['download_attempt']['time']['end_time'] = time();
}
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header("Content-Type: application/octetstream; name=$FILEname"); //for IE & Opera
header("Content-Type: application/octet-stream; name=$FILEname"); //for the rest
header("Content-Disposition: attachment; filename=$FILEname;");
header("Content-Transfer-Encoding: binary");
header("Cache-Control: cache, must-revalidate");
header("Pragma: public");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
readfile($file);
//how can i know the file have been download complete ?? how can i get the end time that when user have fully download ???????
if (isset($_SESSION['download_attempt']['time']['end_time'])){
//case: fully downloaded
$times = $_SESSION['download_attempt']['time'];
$download_time = $times['end_time'] - $times['start_time'];
}
$time = explode(' ',microtime());
$time = (float)$time[0] + (float)$time[1];
//i can't shown the $time in my page ?
}
else
echo 'file doesnt exist';
?>