Tuesday, July 20, 2010

Download File PHP Function

Download File PHP Function


function download($path) {
//global $_SERVER['HTTP_USER_AGENT'];
$file=basename($path);
$size = filesize($path);
header("Content-Type: application/octet-stream");
header("Content-Type: application/force-download");
header("Content-Length: $size");
// IE5.5 just downloads index.php if we don't do this
if(preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {

header("Content-Disposition: filename=$file");
} else {

header("Content-Disposition: attachment; filename=$file");
}

header('Cache-Control: maxage=3600'); //Adjust maxage appropriately
header('Pragma: public');

header("Content-Transfer-Encoding: binary");
$fh = fopen($path, "r");
fpassthru($fh);
}

Remote File Size PHP Function

PHP function for finding remote file size

function remotefilesize($url, $user = "", $pw = "")

{

ob_start();

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_HEADER, 1);

curl_setopt($ch, CURLOPT_NOBODY, 1);

if(!empty($user) && !empty($pw))

{

$headers = array('Authorization: Basic ' . base64_encode("$user:$pw"));

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

}

$ok = curl_exec($ch);

curl_close($ch);

$head = ob_get_contents();

ob_end_clean();

$regex = '/Content-Length:\s([0-9].+?)\s/';

$count = preg_match($regex, $head, $matches);

return isset($matches[1]) ? $matches[1] : "unknown";

}

Tuesday, July 13, 2010

Ago Timestamp PHP function

A php function to find how much time ago the timestamp was

function ago($timestamp){
$difference = time() - $timestamp;
$periods = array("second", "minute", "hour", "day", "week", "month", "years", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
for($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] ago";
return $text;
}

iPad Browser simulator

Hi

Please see this useful website to simulate any website on ipad

http://ipadpeek.com/

Get User Browser PHP function

PHP function to get user browser


function get_user_browser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$ub = '';
if(preg_match('/MSIE/i',$u_agent))
{
$ub = "ie";
}
elseif(preg_match('/Firefox/i',$u_agent))
{
$ub = "firefox";
}
elseif(preg_match('/Safari/i',$u_agent))
{
$ub = "safari";
}
elseif(preg_match('/Chrome/i',$u_agent))
{
$ub = "chrome";
}
elseif(preg_match('/Flock/i',$u_agent))
{
$ub = "flock";
}
elseif(preg_match('/Opera/i',$u_agent))
{
$ub = "opera";
}

return $ub;
}

Resize Image Function

Function to resize an image

function resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){

// create an image of the given filetype
if ($ext == 'jpg' || $ext == 'jpeg'){
if(!function_exists("imagecreatefromjpeg")) return 0;
$image = @imagecreatefromjpeg($from);
}elseif($ext == 'png') {
if(!function_exists("imagecreatefrompng")) return 0;
$image = @imagecreatefrompng($from);

}elseif($ext == 'gif') {
if(!function_exists("imagecreatefromgif")) return 0;

$image = @imagecreatefromgif($from);
}
if(!$image) return 0;

if(function_exists("imagecreatetruecolor")){
$newimg = @imagecreatetruecolor($to_w, $to_h);
}
if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
if(!$newimg){
imagedestroy($image);
return 0;
}

//keep png alpha channel if possible
if($ext == 'png' && function_exists('imagesavealpha')){
imagealphablending($newimg, 0);
imagesavealpha($newimg,true);
}

//try resampling first
if(function_exists("imagecopyresampled")){
if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) {
imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
}
}else{
imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
}

$okay = 0;
if ($ext == 'jpg' || $ext == 'jpeg'){
if(!function_exists('imagejpeg')){
$okay = 0;
}else{
$okay = imagejpeg($newimg, $to);
}
}elseif($ext == 'png') {
if(!function_exists('imagepng')){
$okay = 0;
}else{
$okay = imagepng($newimg, $to);
}
}elseif($ext == 'gif') {
if(!function_exists('imagegif')){
$okay = 0;
}else{
$okay = imagegif($newimg, $to);
}
}

// destroy GD image ressources
if($image) imagedestroy($image);
if($newimg) imagedestroy($newimg);

return $okay;
}

Thursday, January 18, 2007

PHP Function to format currency

Here is a function to format

function format_currency($curr){
$formated_value = number_format($curr, 2, '.', ',');
return $formated_value;
}