Categories

Make a directory

<?php
mkdir (“/htdocs/testdir”, 0644);
echo “Test directory made successfully”;
?>

create a gzip file

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

<?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

<?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

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

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”;
?>