<?
class FileRemoval {
var $fileNameArray, $isRemoved, $errorMsg = '';
function FileRemoval() {
$this->fileNameArray = array();
$this->isRemoved = 0;
}
function getErrorMsg() {
return $this->errorMsg;
}
function getIsRemoved() {
return $this->isRemoved;
}
function setFileNameArray() {
global $argv;
$junk = array_shift($argv); // LOB OFF FIRST ARRAY ELEMENT YOU DON'T NEED THE SCRIPT NAME
$this->fileNameArray = $argv;
}
function remove() {
$this->setFileNameArray();
if (sizeof($this->fileNameArray) == 0) {
$this->isRemoved = 0;
$this->errorMsg = "No files provided for removal\n";
}
if (!$this->errorMsg) {
foreach ($this->fileNameArray as $val) {
if (!file_exists($val) || strlen($val) == 0) {
$this->isRemoved = 0;
$this->errorMsg = "File: $val does not exist\n";
echo $this->errorMsg;
break;
}
}
if (!$this->errorMsg) @unlink($file);
}
if (!$this->errorMsg) $this->isRemoved = 1;
}
function writeErr() {
if ($this->errorMsg) {
$fileID = fopen('./cmds.err', 'r') or die("Could not find error log");
$myErr = '[' . date("%d/%b/%Y:%H:%I:%S"). ']' . $this->errorMsg . "\n";
fputs($fileID, $myErr);
fflush($fileID);
fclose($fileID);
fwrite(STDOUT, $myErr);
}
}
}
$fileRemoval =& new FileRemoval();
$fileRemoval->remove();
echo "**" . $fileRemoval->getIsRemoved();
//if (!$fileRemoval->getIsRemoved()) $this->writeErr();
$fileRemoval = null;
?>
I am writing a simple script that removes a list of files, using class structure to make it more elegant, however, upon any errors found such as "no file found" or "file length empty" or whatever, it should write to an error log file /cmds.err but I have no clue as to how to do that.
I read online about using exec() and passthru() and including this article: http://www.phpcrawler.de/phpmanual/features.commandline.php but so far I see no results in stdout, stderr, basically, nothing works whatsoever.
I don't know what else I can do at this point.
Help!
Phil