Search
Syndication
Sponsors

Archive for the ‘File’ Category

create a gzip file

Sunday, May 24th, 2009

this example backs up the index.php as a gzip file

<?php
$data = implode(”", file(”index.php”));
$gzdata = gzencode($data, 9);
$fp = fopen(”index.php.gz”, “w”);
fwrite($fp, $gzdata);
fclose($fp);
?>

Delete files that are over 7 days old

Thursday, May 14th, 2009

<?php
$dir = ‘/path/to/dir’;
if ($handle = opendir($dir))
{
  while (false !== ($file = readdir($handle)))
  {
    if ($file[0] == ‘.’ || is_dir(”$dir/$file”))
 {
       // ignore hidden files and directories
       continue;
    }
    if ((time() - filemtime($file)) > ($days *86400))
 {
      //note this is using the unlink function
   unlink(”$dir/$file”);
    }
  }
  closedir($handle);
}

?>

File list of a folder

Monday, May 11th, 2009

<?php
//folder to check
$folder = “C:/xampp/htdocs/xampp/wordpress/wp-includes”;
$handle = opendir($folder);
//loop through the directory
while ($file = readdir($handle))
{    
$files[] = $file;
}
closedir($handle); 
//display every file in folder
foreach ($files as $file)
{    
echo “<a href=$folder$file>$file</a>”.”<br />”;
}
?>

Line Counter example

Thursday, March 26th, 2009

This is a simple line counter example which will count the lines in a text file

<?php
function LineCounter($file)
 {
    /* open the file  */
    $fh = fopen( $file, “r” );

    $count = 0;

    while(fgets($fh))
    {
        $count++;
    }
    fclose($fh);
    return $count;
 }
 
 /*change to a file on your system*/
echo LineCounter(”phonebook.php”);

?>

Total Disk and Used Disk space

Saturday, March 14th, 2009

Get the total disk space and used disk space

<?php
$total = disk_total_space(”c:”) / 1048576;
$used = $total - disk_free_space(”c:”) / 1048576;
echo “Total : $total MB<BR>”;
echo “Used: $used MB”;
?>