PHP script can't write to stdout nor to error log

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    PHP script can't write to stdout nor to error log

    <?php

    class FileRemoval {

    var $fileNameArray, $isRemoved, $errorMsg = '';

    function FileRemoval() { // CONSTRUCTOR
    $this->fileNameArra y = array();
    $this->isRemoved = 0;
    }


    // --* GETTER/SETTER METHODS *--


    function getErrorMsg() { // STRING METHOD
    return $this->errorMsg;
    }

    function getIsRemoved() { // BOOLEAN METHOD
    return $this->isRemoved;
    }

    function setFileNameArra y() { // VOID METHOD
    global $argv;
    $junk = array_shift($ar gv); // LOB OFF FIRST ARRAY ELEMENT YOU
    DON'T NEED THE SCRIPT NAME
    $this->fileNameArra y = $argv;
    }

    function remove() { // VOID METHOD
    $this->setFileNameArr ay();
    if (sizeof($this->fileNameArra y) == 0) $this->errorMsg = "No files
    provided for removal\n";
    if (!$this->getErrorMsg( )) {
    foreach ($this->fileNameArra y 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 writeFileNameAr rayMsg() { // VOID METHOD
    if (!$this->fileNameArra y || @sizeof($this->fileNameArra y) == 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->fileNameArra y as $val) {
    $msg .= "\nFile: '$val' has been deleted";
    if (array_search($ val, $this->fileNameArra y) ===
    sizeof($this->fileNameArra y) - 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->writeFileNameA rrayMsg();
    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 writeFileNameAr rayMsg() 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[list of existing files]

    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
Working...