i'm getting this error in my php script Warning: pathinfo() expects parameter 2 to be long, string given in /var/www/html/log.php on line 25 here's the code it's logging file , please help <?php /** * Logging class: * - contains lfile, lwrite and lclose public methods * - lfile sets path and name of log file * - lwrite writes message to the log file (and implicitly opens log file) * - lclose closes log file * - first call of lwrite method will open log file implicitly * - message is written with the following format: [d/M/Y:H:i:s] (script name) message */ class Logging { // declare log file and file pointer as private properties private $log_file, $fp; // set log file (path and name) public function lfile($path) { $this->log_file = $path; } // write message to the log file public function lwrite($message) { // if file pointer doesn't exist, then open log file if (!is_resource($this->fp)) { $this->lopen(); } // define script name $script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME); // define current time and suppress E_WARNING if using the system TZ settings // (don't forget to set the INI setting date.timezone) $time = @date('[d/M/Y:H:i:s]'); // write current time, script name and message to the log file fwrite($this->fp, "$time ($script_name) $message" . PHP_EOL); } // close log file (it's always a good idea to close a file when you're done with it) public function lclose() { fclose($this->fp); } // open log file (private method) private function lopen() { // in case of Windows set default log file if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $log_file_default = 'logs.txt'; } // set default log file for Linux and other systems else { $log_file_default = 'logs.txt'; } // define log file from lfile method or use previously set default $lfile = $this->log_file ? $this->log_file : $log_file_default; // open log file for writing only and place file pointer at the end of the file // (if the file does not exist, try to create it) $this->fp = fopen($lfile, 'a') or exit("Can't open $lfile!"); } } ?>
First, learn to use CODE-brackets around your code. Second, try the following: $script_name = pathinfo($_SERVER['PHP_SELF']); ... // write current time, script name and message to the log file fwrite($this->fp, "$time ($script_name['filename']) $message" . PHP_EOL); } Code (markup): I'm not sure it will work, not tested, but if you read up on pathinfo() here: http://php.net/manual/en/function.pathinfo.php it seems it should work...
Thank you, there's a syntax error Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in log.phpon line30 fwrite($this->fp, "$time ($script_name['filename']) $message" . PHP_EOL);
No - it's because you're trying to output more than one variable at a time - change it to: fwrite($this->fp, "$time.' '.$script_name['filename'].' '.$message".PHP_EOL);