Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

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";

}

Thursday, January 18, 2007

PHP function to get extension of a file

Here is a simple usefull function to get extension of a file

function getFileExtension($filename){
$dot = substr (strrchr ($filename, "."), 1);
return ($dot);
}