Showing posts with label PHP Files. Show all posts
Showing posts with label PHP Files. Show all posts

25 July, 2011

Caching in PHP

The term Caching refers to storing of data for later usage. Commonly used data can be cached it can be accessed faster and caching in PHP can be easier task as well, if you know what you are doing. Following code snippet demonstrates File Caching in php in which data is written to the file by caching process.

class CacheFile {
protected $fPointer;
protected $fileName;
protected $expirationTime;
protected $tempFileName;

//constructor function
//pass in the filename along with optional expiration
public function __construct($fileName, $expiration = false) {
$this->fileName = $fileName;
$this->expirationTime = $expiration;
$this->tempFileName = "$filename.".getmypid();
}

public function get() {
//check for expiration and return contents of cache file
if($this->expirationTime) {
//get the file status
$status = @stat($this->fileName);
if($status[9]) {
//check the filetime
if(($modified + $this->expirationTime) < time()) {
@unlink($this->fileName);
return false;
}
}
}
return file_get_contents($this->fileName);
}

public function put($fileBuffer) {
if(($this->fPointer == fopen($this->tempFileName, 'w')) == false) {
return false;
}
fwrite($this->fPointer, $fileBuffer);
fclose($this->fPointer);
rename($this->tempFileName, $this->fileName);
return true;
}

//remove the cache file
public function remove() {
@unlink($this->fileName);
}

//start the caching process
public function startOutputBuffer() {
if(($this->fPointer = fopen($this->tempFileName, 'w')) == false) {
return false;
}
ob_start();
}

//end the caching process
public function endOutputBuffer() {
//get the output bufer contents
$bufferContents = ob_get_contents();
ob_end_flush();
if(strlen($bufferContents)) {
fwrite($this->fPointer, $bufferContents);
fclose($this->fPointer);
rename($this->tempFileName, $this->fileName);
return true;
}
else {
fclose($this->fPointer);
@unlink($this->tempFileName);
return false;
}
}
}
?>

And the File Cache class cane be used as follows:

require_once('CacheFile.php');
$cacheData = CacheFile($pathToCacheFile);
//check if the string exists in cache
if($string = $cacheData->get()) {
var_dump($string);
}
else {
//start caching process
$cacheData->startOutputBuffer();
?>
//your page data included here
$cacheData->endOutputBuffer();
}
?>

In this way we can implement Caching in php.

08 December, 2010

Get all files inside a directory using php

Listing of files inside a directory can be done simply using php.
Following function can be used to list all the files inside a directory.

function list_directory($dirpath) {
$files = array(); //files array
$dirs = array(); //directories array
$file_num = 0; //set number of files to 0
$dir_num = 0; //set number of directories to 0
//check if the path is a directory
if (is_dir($dirpath)) {
//create a handle for opening directory
$hndl = opendir($dirpath);
//start the loop
do {
//read the directory
$fle = readdir($hndl);
if ($fle !== FALSE && $fle != "." && $fle != "..") {
//check if its a directory or a file
if (is_dir("$dirpath/$fle")) $dirs[$dir_num++] = $fle;
else $files[$file_num++] = $fle;
}
} while($fle !== FALSE);
closedir($hndl);
}
return array($dir_num, $file_num, $dirs, $files);
}

08 November, 2010

Get path of a file in PHP

Once i have to get location of a file using a single line of code in php. Here's how i did it.

$filePath = substr($_SERVER["SCRIPT_FILENAME"], 0, strrpos($_SERVER["SCRIPT_FILENAME"], "/"));
echo $filePath; // this gives the full file path

Hope it helps someone.

08 September, 2010

Get only the path for a file

Sometimes it is required the get only the path that a file exists. In such cases following function could be of some help. I
hope this function helps someone like me

function getPath($path) {
if(substr($path, -1, 1) == '/') {
return $path;
}
else {
$pathArr = explode('/', $path);
$total = count($pathArr);
$end = $pathArr[$total - 1];
if(substr_count($end, '.') > 0) {
array_pop($pathArr);
}
$finalPath = implode('/', $pathArr);
return $finalPath;
}
}
?>

04 May, 2010

Access folder inside nested folders in PHP


//Access nested folders contents using PHP
<?php
function getDir($id=0,$maxLvl=20){
$id1=$id;
$id=str_pad($id,$maxLvl,0,STR_PAD_LEFT);
$dir=NULL;
for($i=0;$i<$maxLvl-2;$i=$i+2){
$dir.=substr($id,$i,2)."/";
$dir1=substr($id,$i,2);
if(!file_exists($dir1))
mkdir($dir1);
chdir($dir1);
}
if(!file_exists($id1))

mkdir($id1);

return $dir.$id1;
}
?>

Create nested folders using PHP


Create maximum levels of nested folders using PHP
<?php
function getWDir($id=0,$maxLvl=20){
$id1=$id;
//add 0 to the left
$id=str_pad($id,$maxLvl,0,STR_PAD_LEFT);
$dir=NULL;
for($i=0;$i<$maxLvl-2;$i=$i+2){
//get the sub-strings for generating multiple folders
$dir.=substr($id,$i,2)."/";
$dir1=substr($id,$i,2);
}
return $dir;
}
?>

05 July, 2009

File extension in PHP

For getting file extension, simply this can be done:
$fileName = "SampleOne.jpg";
$fileExtension = array_pop(explode(".", $fileName));