<?php
class FileRemoval {
var $fileNameArray, $isRemoved, $errorMsg = '';
function FileRemoval() { // CONSTRUCTOR
$this->fileNameArray = array();
$this->isRemoved = 0;
}
// --* GETTER/SETTER METHODS *--
function getErrorMsg() { // STRING METHOD
return $this->errorMsg;
}
function getIsRemoved() { // BOOLEAN METHOD
return $this->isRemoved;
}
function setFileNameArray() { // VOID METHOD
global $argv;
$junk = array_shift($argv); // LOB OFF FIRST ARRAY ELEMENT YOU DON'T NEED THE SCRIPT NAME
$this->fileNameArray = $argv;
}
function remove() { // VOID METHOD
$this->setFileNameArray();
if (sizeof($this->fileNameArray) == 0) $this->errorMsg = "No files provided for removal\n";
if (!$this->getErrorMsg()) {
foreach ($this->fileNameArray as $val) {
if (!file_exists($val) || strlen($val) == 0) {
$this->errorMsg = "File: '$val' does not exist ";
break;
}
@unlink($val);
}
if (!$this->getErrorMsg()) $this->isRemoved = 1;
}
}
function writeErr() { // VOID METHOD
if ($this->getErrorMsg()) {
$myErr = '[' . date("d/M/Y:H:i:s"). '] ' . $this->errorMsg;
$fileID = @fopen('./cmds.err', 'a') or die("Could not find error log");
fputs($fileID, $myErr); fflush($fileID); fclose($fileID);
$fileID = @fopen('php://stdout', 'a') or die("Could not open STDOUT");
fputs($fileID, $myErr); fflush($fileID); fclose($fileID);
}
}
function writeFileNameArrayMsg() { // VOID METHOD
if (!$this->fileNameArray || @sizeof($this->fileNameArray) == 0) {
$this->errorMsg = "There are no files to remove\n";
$fileID = @fopen('php://stdout', 'a') or die("Could not open STDOUT");
fputs($fileID, $this->getErrorMsg()); fflush($fileID); fclose($fileID);
} else {
foreach ($this->fileNameArray as $val) {
$msg .= "\nFile: '$val' has been deleted";
if (array_search($val, $this->fileNameArray) === sizeof($this->fileNameArray) - 1) $msg .= "\n";
}
$fileID = @fopen('php://stdout', 'a') or die("Could not open STDOUT");
fputs($fileID, $msg); fflush($fileID); fclose($fileID);
}
}
}
// ----- END OF CLASS -----------------------------------------------------
// MAIN SCRIPT - INSTANTIATE A FileRemoval CLASS OBJECT INSTANCE AND RUN THE remove() METHOD
$hasPrintedErr = false;
$fileRemoval =& new FileRemoval();
$fileRemoval->remove();
if (!$fileRemoval->getIsRemoved()) {
$fileRemoval->writeErr();
$hasPrintedErr = true;
}
$fileRemoval->writeFileNameArrayMsg();
if ($fileRemoval->getErrorMsg() && !$hasPrintedErr) $fileRemoval->writeErr();
$fileRemoval = null;
?>
I have done everything I can think of to get messages to print using either writeErr() method or writeFileNameArrayMsg() method, however, nothing appears in stdout nor in cmds.err no matter what I do. The PHP script is a command-line PHP script called by the following:
php -q /../fileremoval.php $fileList
And I can verify that the files are being removed by the remove() method every time, that works. But no messages ever appear either in stdout nor in the error log indicating anything, no matter what I do, nothing appears. However, if I call the file from the command line prompt:
php -q /./fileremoval.php
It works just fine and produces messages. But I am calling this script from 2 other scripts:
cmds.sh -a --transferfiles --delete -d /me/dummy
Which calls
tclsh /../transfer/archive.tcl"
which it calls
eval "exec php -q /../fileremoval.php $fileList
I traced all of my coding to indicate that everything functions properly, except in the case of the PHP script simply never producing any output anywhere unless I call it directly, once again, from the command line prompt.
I could honestly use some extra pairs of eyes to view my PHP code and tell me if the code is wrong, or if there is some way that stdout and/or stderr >> /cmds.err has been somehow either redirected or overwritten by something else. I speak very little "techie" so that's the best I can explain it to anyone.
Help!
Thanx
Phil