leon_pegg
Joined: 12 Jun 2006 Posts: 113 Location: hemel / UK
|
Posted: Tue Jun 27, 2006 1:12 am Post subject: php-gtk2 file grabber |
|
|
This is the code to a basic file grabber program.
this is part of a larger program i am writing to apply updates and extentions to Gnope (windows) and php.
hope someone finds this code helpful contians some useful functions.
sec2hms() converts seconds to h:mm:ss format
remote_file_size ($url) gets file size of remote file
the base of this code is a progressbar example i found in the forum so this is more of a testing bed for the final code.
| Code: | <?php
function bytes_scale($bytes)
{
if ($bytes < 1024 )
return array($bytes,'B',$bytes);
elseif ($bytes >= 1024 && $bytes < 1048576 )
return array(round($bytes/1024, 2),'kB',$bytes);
elseif ($bytes >= 1048576 && $bytes < 1073741824 )
return array(round($bytes/1048576, 2),'MB',$bytes);
else
return array(round($bytes/1073741824, 2),'GB',$bytes);
}
function remote_file_size ($url)
{
$head = "";
$url_p = parse_url($url);
$host = $url_p["host"];
$path = $url_p["path"];
$fp = fsockopen($host, 80, $errno, $errstr, 20);
if(!$fp)
{ return false; }
else
{
fputs($fp, "HEAD ".$url." HTTP/1.1\r\n");
fputs($fp, "HOST: dummy\r\n");
fputs($fp, "Connection: close\r\n\r\n");
$headers = "";
while (!feof($fp)) {
$headers .= fgets ($fp, 128);
}
}
fclose ($fp);
$return = false;
$arr_headers = explode("\n", $headers);
foreach($arr_headers as $header) {
$s = "Content-Length: ";
if(substr(strtolower ($header), 0, strlen($s)) == strtolower($s)) {
$return = substr($header, strlen($s));
break;
}
}
return $return;
}
if(!class_exists('gtk'))
{
die("Please load the php-gtk2 module in your php.ini\r\n");
}
function quit_routine($dialog)
{
$dialog->destroy();
}
function test_progress() {
global $window;
$window->hide_all();
$from = "http://ftp.ticklers.org/releases.ubuntu.org/releases/6.06/ubuntu-6.06-desktop-i386.iso";
$to ="ubuntu.iso";
$window1 = new GtkWindow();
$window1->set_title("Progress");
$window1->set_size_request(200, 55);
$progress_bar = new GtkProgressBar();
$progress_bar->set_orientation(Gtk::PROGRESS_LEFT_TO_RIGHT);
$window1->add($progress_bar);
$window1->connect("destroy", "quit_routine");
$window1->show_all();
$handle = fopen($from, 'r');
$totalsize = remote_file_size($from);
$total = bytes_scale($totalsize);
$Data = null;
$data1 = null;
$fh = fopen($to, "wb");
define('_START', microtime());
while ($data1 < $totalsize) {
$downloaded = bytes_scale($data1);
$finish = microtime();
list($secs, $usecs) = explode(' ', _START);
$start = $secs + $usecs ;
list($secs, $usecs) = explode(' ', $finish);
$finish1 = $secs + $usecs ;
$time = round($finish1 - $start);
@$speed = bytes_scale($data1 / $time);
$speed[0] = round($speed[0]);
$per = $totalsize / 100;
$curper = round($data1 / $per,1);
$progress_bar->set_fraction((float)($curper/100));
$progress_bar->set_text("$curper % of $total[0] $total[1]\nTime: ".sec2hms($time)."\nAvrage Speed : $speed[0] $speed[1]/Sec");
while (Gtk::events_pending()) {
Gtk::main_iteration();
}
$Data = fread($handle,1020);
fwrite($fh, $Data);
$data1 = $data1 + strlen($Data);
}
fclose($fh);
fclose($handle);
//quit_routine($window1);
}
function sec2hms ($sec, $padHours = false)
{
$hms = "";
$hours = intval(intval($sec) / 3600);
$hms .= ($padHours)
? str_pad($hours, 2, "0", STR_PAD_LEFT). ':'
: $hours. ':';
$minutes = intval(($sec / 60) % 60);
$hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':';
$seconds = intval($sec % 60);
$hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
return $hms;
}
$window = new GtkWindow();
$window->set_title("Progress Bar Test");
$window->set_size_request(200, 100);
$button = &new GtkButton('Click for Progress Bar');
$button->connect_simple('clicked', 'test_progress');
$button->show();
$window->add($button);
$window->connect_simple("destroy", array("gtk", "main_quit"));
$window->show_all();
Gtk::main();
?> |
hope you enjoy
regards leon pegg |
|