469,133 Members | 1,125 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,133 developers. It's quick & easy.

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

[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
Jul 17 '05 #1
3 3387
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" <so*****@erols.com> napisal w wiadomosci
news:1c**************************@posting.google.c om...
[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

Jul 17 '05 #2

"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
<SNIP CODE>
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.


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
Jul 17 '05 #3

"Anthony Borla" <aj*****@bigpond.com> wrote in message
news:KV******************@news-server.bigpond.net.au...

"Phil Powell" <so*****@erols.com> wrote in message
news:1c**************************@posting.google.c om...
<SNIP CODE>

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.


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

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
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Sveta | last post: by
2 posts views Thread by Paul A. Wilson | last post: by
10 posts views Thread by Siemel Naran | last post: by
3 posts views Thread by Michael Roebuck | last post: by
11 posts views Thread by jobs239 | last post: by
2 posts views Thread by luanhoxung | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.