Re: Re: fwrite/fclose troubles

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Peter Ford wrote:
Mark Weaver wrote:
Hi all,

I've been lurking and reading now for some time, but have decided to
come out of the shadows cause I've got an issue that's gonna drive me crazy!

I'm developing an application and within this application is a class
that is very simple and only serves a singular purpose - to make log
entries to help with debugging. Problem is, now I'm debugging the damned
logging class that is supposed to be helping me debug the application as
I'm putting it together! <sigh> I've looked and looked all over the
place, but I don't seem to be able to find an answer to this problem.
The only information that I have found so far deals with permissions and
I don't think that's the problem. At first I was getting an access
denied error but since setting dir perms and log file perms so that both
apache and my user can right to both the directory and the file that one
has gone away.

Log Directory permissions: /mystuff/logs rwx-rwx-rwx (777)
Log file permissions            : /mystuff/logs/run.log   rwx-rwx-rwx
(777)

At any rate, the following is the information I'm getting in the apache
error_log while working on this particular portion of the application:

PHP Warning:  fwrite(): supplied argument is not a valid stream resource
in /mystuff/inc/Log.inc on line 22,
PHP Warning:  fclose(): supplied argument is not a valid stream resource
in /mystuff/inc/Log.inc on line 23,

The Log class:
-----------------------------
class Log{
    public $path, $entry, $logfile;
        public function Log(){}
        public function setLog($path,$file){
        $this->path = $path;
        $this->logfile = $file;
    }
        public function writeLog($entry){
        // open the file, in this case the log file
        $h = "$this->path/$this->logfile";
        fopen($h, 'a+');
        fwrite($h,$entry);
        fclose($h);
    }
}

Code snippet where attempting to write log entry from program:
--------------------------------------------------------------------------------------------
        $pl_log = new Log;
        $pl_log->setLog($logpath,"run.log");
               $usernanme = $_POST['username'];
        $password = $_POST['secret'];
               /**
           * (debugging)     logging incoming values from form:
           */
$pl_log->writeLog("getDateTime(): Incoming values from Login Form:
blah...blah...blah\n");

Any help with this would be most appreciated. (be gentle... I'm a PERL
program learning PHP OOP)



As Stut pointed out, you've misunderstood the difference between a file resource and a file name.

Try something like:


     public function writeLog($entry)
     {
         // open the file, in this case the log file
         $fileName = $this->path.'/'.$this->logfile;
         $h = fopen($fileName, 'a+');
         fwrite($h,$entry);
         fclose($h);
     }

The file resource that fwrite and fclose need is the *result* of opening the file, not the file name as you were doing. Ideally, you would check the value of $h after the fopen call, to make sure that it had successfully opened the file.

Also, I changed the way you were constructing the file name: interpolating the variables in a string is slightly less efficient than concatenating the bits together, and there are possible gotchas when using the $this->variable structure in a string like that.
An alternative syntax is to escape the variables with braces:

    $fileName = "{$this->path}/{$this->logfile}";

Hi Peter,

Thank you for that information. I'm rather new at this sort of PHP coding and a bit embarrassed at the amount of OOP I've forgotten since college. Clearly there's a lot to learn. Like this concept of escaping you've mentioned. Until now the only type of "escaping" I'm familiar with from PERL is something like this: \$$some_dollar_amount, but I have a feeling escaping things in PHP oop is quite a bit different. I haven't quite gotten my head around some of the stuff I've been reading about, but it's coming.

--
Mark

"If you have found a very wise man, then you've found a man that at one time was an idiot and lived long enough to learn from his own stupidity."
==============================================
Powered by CentOS5 (RHEL5)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux