note 56378 added to ref.fam
| From: | YassinEzbakhe | Date: | Wed, 31 Aug 2005 23:16:11 +0000 |
| Subject: | note 56378 added to ref.fam | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94556@lists.php.net to get a copy of this message | ||
I make a VERY simple class that monitors a folder (and its subfolders) for new or removed files. I
use it in order to auto index a folder where I have all my eBooks into a MySQL database.
CLASS FILE:
<?php
class FileAlterationMonitor
{
private $scanFolder, $initialFoundFiles;
public function __construct($scanFolder)
{
$this->scanFolder = $scanFolder;
$this->updateMonitor();
}
private function _arrayValuesRecursive($array)
{
$arrayValues = array();
foreach ($array as $value)
{
if (is_scalar($value) OR is_resource($value))
{
$arrayValues[] = $value;
}
elseif (is_array($value))
{
$arrayValues = array_merge( $arrayValues, $this->_arrayValuesRecursive($value));
}
}
return $arrayValues;
}
private function _scanDirRecursive($directory)
{
$folderContents = array();
$directory = realpath($directory).DIRECTORY_SEPARATOR;
foreach (scandir($directory) as $folderItem)
{
if ($folderItem != "." AND $folderItem != "..")
{
if (is_dir($directory.$folderItem.DIRECTORY_SEPARATOR))
{
$folderContents[$folderItem] = $this->_scanDirRecursive(
$directory.$folderItem."\\");
}
else
{
$folderContents[] = $folderItem;
}
}
}
return $folderContents;
}
public function getNewFiles()
{
$finalFoundFiles = $this->_arrayValuesRecursive(
$this->_scanDirRecursive($this->scanFolder));
if ($this->initialFoundFiles != $finalFoundFiles)
{
$newFiles = array_diff($finalFoundFiles, $this->initialFoundFiles);
return empty($newFiles) ? FALSE : $newFiles;
}
}
public function getRemovedFiles()
{
$finalFoundFiles = $this->_arrayValuesRecursive(
$this->_scanDirRecursive($this->scanFolder));
if ($this->initialFoundFiles != $finalFoundFiles)
{
$removedFiles = array_diff( $this->initialFoundFiles, $finalFoundFiles);
return empty($removedFiles) ? FALSE : $removedFiles;
}
}
public function updateMonitor()
{
$this->initialFoundFiles = $this->_arrayValuesRecursive($this->_scanDirRecursive(
$this->scanFolder));
}
}
?>
A simple script that uses this class could be like this one (use it with PHP CLI):
<?php
$f = new FileAlterationMonitor($MY_FOLDER_TO_MONITOR)
while (TRUE)
{
sleep(1);
if ($newFiles = $f->getNewFiles())
{
// Code to handle new files
// $newFiles is an array that contains added files
}
if ($removedFiles = $f->getRemovedFiles())
{
// Code to handle removed files
// $newFiles is an array that contains removed files
}
$f->updateMonitor();
}
----
Manual Page -- http://www.php.net/manual/en/ref.fam.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56378
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56378&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56378&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56378&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56378&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56378&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56378&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56378&report=yes
Search -- http://master.php.net/manage/user-notes.php