473,657 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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->fileNameArra y = array();
$this->isRemoved = 0;
}

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

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

function setFileNameArra y() {
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() {
$this->setFileNameArr ay();
if (sizeof($this->fileNameArra y) == 0) {
$this->isRemoved = 0;
$this->errorMsg = "No files provided for removal\n";
}
if (!$this->errorMsg) {
foreach ($this->fileNameArra y 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 3569
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.goo gle.com...
[PHP]
<?php

class FileRemoval {

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

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

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

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

function setFileNameArra y() {
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() {
$this->setFileNameArr ay();
if (sizeof($this->fileNameArra y) == 0) {
$this->isRemoved = 0;
$this->errorMsg = "No files provided for removal\n";
}
if (!$this->errorMsg) {
foreach ($this->fileNameArra y 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.goo gle.com...
<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*****@bigpon d.com> wrote in message
news:KV******** **********@news-server.bigpond. net.au...

"Phil Powell" <so*****@erols. com> wrote in message
news:1c******** *************** ***@posting.goo gle.com...
<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
4052
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 application works fine on some devices. But at Samsung mobile phone, I have a problem. In this phone my 2 commands appear under menu command, and then I can
2
3240
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. My button bar is implemented a Frame subclass, which takes the button dictionary as an argument and displays the buttons on the screen: class OptionsBar(Frame): def __init__(self, buttonDict, parent=None) Frame.__init__(self, parent)
2
2473
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 Command center but this facility does not seem to exist for System commands. Seems command Window and CLP are the only places where we can interactively issue these commands..darn it!!! Regards, Raquel.
10
16484
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 newsgroups I learned that there is a function in stdlib.h called system. int system(const char *command); First question, is the system command ANSI compliant. Because I include <cstdlib> and write std::system(command.c_str()); it looks like an...
14
4960
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 for things like delete, save, edit, cancel buttons - in the footer, or on the form detail section? 2. If in the footer, how do you add them to the tab order?
3
4841
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 switches! Any ideas examples gratefully accepted.
11
2722
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
1487
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 my office, i can see: cm as Command, and everything do as my intent (cm is a Command)--->Why ? Please give me a hand. thanks in advance. Luan from VietNam
3
4699
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 but eventually i wrote out the entire command my self, and still nothing. The only thing that i can think this that because i am trying to add a table that is created outside the data adaptor and then Merge/Copy the data into
0
8402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8315
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8829
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8734
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8508
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6172
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4164
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.