473,320 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 3553
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sveta | last post by:
Hi, all! I am new with J2ME. I have application that has form with 2 commands (exit and select). All UI uses are high level API, and it is very important to leave it high-level. This...
2
by: Paul A. Wilson | last post by:
I'm new to Tkinter programming and am having trouble creating a reusable button bar... I want to be able to feed my class a dictionary of button names and function names, which the class will make....
2
by: Raquel | last post by:
Why can we not give db2 System commands (commands starting with 'db2' like db2ilist, db2look, db2move etc.) through the Command Center? It is so convenient to retrieve and change commands in the...
10
by: Siemel Naran | last post by:
Hi. I'm writing a command shell that reads commands from standard input. At this point I have the command in a std::string. Now I want to execute this command in the shell. From the Borland...
14
by: Kevin | last post by:
A couple of easy questions here hopefully. I've been working on two different database projects which make use of multiple forms. 1. Where's the best/recommended placement for command buttons...
3
by: Michael Roebuck | last post by:
Hi all I'm very new to VB - I am trying to run a DOS command from within a VB2005 asp.net web site using a command button? The DOS command will take the form of the command followed by several...
11
by: jobs239 | last post by:
Can I use this line inside C program "system(java -jar <jarfilename>)" to run a java program from C? Or do I have to use some JNI interface.?
2
by: luanhoxung | last post by:
Hi, Folks Please show me what happen ? In my Code, I declare: cm as command the word "command" doesnot capital the first letter like : Command. And i think VBA doesnot know what is cm ?? But in...
3
by: iKiLL | last post by:
Hi all I am having problems getting my SqlCeDataAdapter to Update the SQL Mobile Data base. i am using C# CF2. I have tried this a number of different ways. Starting with the command builder...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.