Re: fwrite/fclose troubles
| From: | Peter Ford | Date: | Thu, 20 Mar 2008 09:37:05 +0000 |
| Subject: | Re: fwrite/fclose troubles | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-271882@lists.php.net to get a copy of this message | ||
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.As Stut pointed out, you've misunderstood the difference between a file resource and a file name. Try something like: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)
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}";
--
Peter Ford phone: 01580 893333
Developer fax: 01580 893399
Justcroft International Ltd., Staplehurst, Kent