Thursday, August 27, 2009

File size in human format

I needed to see some file sizes in a human format (in a PHP page) so I created this function some of you might find useful:


/**Returns the size of data (provided in bytes) in a human readable
* format
* @size - the the size of data in bytes
* @author alex from scriptoid.com
*/
function humanSize($size){
if($size < pow(1024, 1)){
return $size . 'Bytes';
}
else if ($size < pow(1024, 2)){
return sprintf("%.2f", $size/pow(1024, 1)) . 'Kb'; //Kilo
}
else if ($size < pow(1024, 3)){
return sprintf("%.2f", $size/pow(1024, 2)) . 'Mb'; //Mega
}
else if ($size < pow(1024, 4)){
return sprintf("%.2f",$size/pow(1024, 3)) . 'Gb'; //Giga
}
else if ($size < pow(1024, 5)){
return sprintf("%.2f",$size / pow(1024, 4)) . 'Tb'; //Tera
}
else if ($size < pow(1024, 6)){
return sprintf("%.2f",$size / pow(1024, 5)) . 'Pb'; //Peta
}
else if ($size < pow(1024, 7)){
return sprintf("%.2f",$size / pow(1024, 6)) . 'Eb'; //Exa
}
else if ($size < pow(1024, 8)){
return sprintf("%.2f",$size /pow(1024, 7)) . 'Zb'; //Zeta
}
else if ($size < pow(1024, 9)){
return sprintf("%.2f",$size /pow(1024, 8)) . 'Yb'; //Yota
}
else{
return 'too big'; //:D
}
}

Sunday, August 23, 2009

Low level HTTP(s) testing

For http use:
$ telnet localhost 80
GET / HTTP/1.0

For https use (openssl tool):
$ openssl s_client -connect localhost:443 -state -debug
GET / HTTP/1.0

Before the actual HTTP response you will receive detailed information about the SSL handshake.


More...ff you want to use HTTP/1.1 use
GET / HTTP/1.1

Even More...if you use virtual servers enter (after you get a prompt from GET / HTTP/1.1)
Host: your_virtual_server_name