473,372 Members | 941 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,372 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 3556
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.