Connecting Tech Pros Worldwide Forums | Help | Site Map

command-line PHP script fails to write to error log and to stdout - need help

Phil Powell
Guest
 
Posts: n/a
#1: Jul 17 '05
[PHP]
<?php

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()) $fileRemoval->writeErr();
$fileRemoval = null;

?>
[/PHP]

I am writing a very simple PHP script that will be called from a
front-end bash script (and a TCL script - long story) that will remove
a list of files passed into the PHP script as $argv array. Problem is
right now that I am unable to get it to write to the error log (see
method writeErr) nor am I able to produce the error in stdout (again
see method writeErr), I get the error message:

Supplied argument is not a valid File-Resource handle in line 53:

line 53: fwrite(STDOUT, $myErr);

I am not sure now how to do this so I can use some help.

Thanx
Phil

Chung Leong
Guest
 
Posts: n/a
#2: Jul 17 '05

re: command-line PHP script fails to write to error log and to stdout - need help


If you open your file for reading, of course you can't write to it. Pass
"a+" as the second argument to fopen() for appending to a file.

And there is not STDOUT keyword in PHP. Do an echo if you want to output
something to stdout.

Uzytkownik "Phil Powell" <soazine@erols.com> napisal w wiadomosci
news:1cdca2a7.0401031303.6bee67d0@posting.google.c om...[color=blue]
> [PHP]
> <?php
>
> 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()) $fileRemoval->writeErr();
> $fileRemoval = null;
>
> ?>
> [/PHP]
>
> I am writing a very simple PHP script that will be called from a
> front-end bash script (and a TCL script - long story) that will remove
> a list of files passed into the PHP script as $argv array. Problem is
> right now that I am unable to get it to write to the error log (see
> method writeErr) nor am I able to produce the error in stdout (again
> see method writeErr), I get the error message:
>
> Supplied argument is not a valid File-Resource handle in line 53:
>
> line 53: fwrite(STDOUT, $myErr);
>
> I am not sure now how to do this so I can use some help.
>
> Thanx
> Phil[/color]


Anthony Borla
Guest
 
Posts: n/a
#3: Jul 17 '05

re: command-line PHP script fails to write to error log and to stdout - need help



"Phil Powell" <soazine@erols.com> wrote in message
news:1cdca2a7.0401031303.6bee67d0@posting.google.c om...[color=blue]
>[/color]
<SNIP CODE>[color=blue]
>
> I am writing a very simple PHP script that will be called from a
> front-end bash script (and a TCL script - long story) that will remove
> a list of files passed into the PHP script as $argv array. Problem is
> right now that I am unable to get it to write to the error log (see
> method writeErr) nor am I able to produce the error in stdout (again
> see method writeErr), I get the error message:
>
> Supplied argument is not a valid File-Resource handle in line 53:
>
> line 53: fwrite(STDOUT, $myErr);
>
> I am not sure now how to do this so I can use some help.
>[/color]

Sounds like your're using a PHP version older than 4.3.0 where STDOUT et al,
are not automatically opened. You could do this yourself, using something
like:

define('STDOUT', fopen("php://stdout", "r"));
define('STDERR', fopen("php://stderr", "r"));
...

See:

http://www.php.net/manual/en/features.commandline.php

for more details.

I hope this helps.

Anthony Borla


Phil Powell
Guest
 
Posts: n/a
#4: Jul 17 '05

re: command-line PHP script fails to write to error log and to stdout - need help



"Anthony Borla" <ajborla@bigpond.com> wrote in message
news:KVMJb.76752$aT.51366@news-server.bigpond.net.au...[color=blue]
>
> "Phil Powell" <soazine@erols.com> wrote in message
> news:1cdca2a7.0401031303.6bee67d0@posting.google.c om...[color=green]
> >[/color]
> <SNIP CODE>[color=green]
> >
> > I am writing a very simple PHP script that will be called from a
> > front-end bash script (and a TCL script - long story) that will remove
> > a list of files passed into the PHP script as $argv array. Problem is
> > right now that I am unable to get it to write to the error log (see
> > method writeErr) nor am I able to produce the error in stdout (again
> > see method writeErr), I get the error message:
> >
> > Supplied argument is not a valid File-Resource handle in line 53:
> >
> > line 53: fwrite(STDOUT, $myErr);
> >
> > I am not sure now how to do this so I can use some help.
> >[/color]
>
> Sounds like your're using a PHP version older than 4.3.0 where STDOUT et[/color]
al,[color=blue]
> are not automatically opened. You could do this yourself, using something
> like:
>
> define('STDOUT', fopen("php://stdout", "r"));
> define('STDERR', fopen("php://stderr", "r"));
> ...
>
> See:
>
> http://www.php.net/manual/en/features.commandline.php
>
> for more details.
>
> I hope this helps.
>
> Anthony Borla
>
>[/color]


Actually none of that worked for me, including define(). The solution was a
bit esoteric and I don't have a technical explanation for it, but in the TCL
script that calls the PHP code I did this

set blah [eval "exec php -q /./fileremoval.php $fileList]
puts $blah

Apparently the TCL script that calls the PHP script might have control of
STDOUT so there was nothing that PHP could do to output its results, so I
simply allowed for any results to go to the TCL script variable $blah that
would be set to whatever the PHP script sends it.

I can't explain it any better than that.

Phil


Closed Thread