Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 516597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:44:52+00:00 2026-05-13T07:44:52+00:00

I found a logging script like this: /** * Logging class: * – contains

  • 0

I found a logging script like this:

/** 
 * Logging class: 
 * - contains lopen and lwrite methods 
 * - lwrite will write message to the log file 
 * - first call of the lwrite will open log file implicitly 
 * - message is written with the following format: hh:mm:ss (script name) message 
 */  
class Logging{  
  // define log file  
  private $log_file = '/tmp/logfile.txt';  
  // define file pointer  
  private $fp = null;  
  // write message to the log file  
  public function lwrite($message){  
    // if file pointer doesn't exist, then open log file  
    if (!$this->fp) $this->lopen();  
    // define script name  
    $script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);  
    // define current time  
    $time = date('H:i:s');  
    // write current time, script name and message to the log file  
    fwrite($this->fp, "$time ($script_name) $message\n");  
  }  
  // open log file  
  private function lopen(){  
    // define log file path and name  
    $lfile = $this->log_file;  
    // define the current date (it will be appended to the log file name)  
    $today = date('Y-m-d');  
    // open log file for writing only; place the file pointer at the end of the file  
    // if the file does not exist, attempt to create it  
    $this->fp = fopen($lfile . '_' . $today, 'a') or exit("Can't open $lfile!");  
  }  
} 

I can’t see any file closing code here. Is that just fine? If not, at which point would that be appropriate? Or does PHP close the file anyways as soon as the script is completely executed (run through)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:44:52+00:00Added an answer on May 13, 2026 at 7:44 am

    As people have said, PHP will close the file automatically. However, it is best practice to close the file.

    1. In most languages you have to explicitly close your files, and failure to do so may lead to data loss. So it’s a good habit to be in, and helpful if you ever port your application.

    2. Data may not be flushed to the disk until the close happens. If your script runs for any length of time ( granted, as a PHP script it probably won’t ) then you might want more explicit control on when things are written. For my “typical” logging application, I open and close the file on each line, so that my log is saved in the event of a crash

    3. The most important, and overlooked: It stops the next person from having to ask this question again! This is a code clarity issue; if you have to ask if it’s right, you should consider rewriting it so that it looks right.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 304k
  • Answers 304k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer On SQL Server 2005 and up: connect to that specific… May 13, 2026 at 8:50 pm
  • Editorial Team
    Editorial Team added an answer The Mach-O object file format used by Mac OS X… May 13, 2026 at 8:50 pm
  • Editorial Team
    Editorial Team added an answer You need to set the scanIntervalSeconds to a value greater… May 13, 2026 at 8:50 pm

Related Questions

Does anyone know of a Webmail Contact List Importer scripts (ColdFusion, PHP etc) like
TL;DR: I've supplied a patch for a bug I found and I've got 0
I'm having a problem implementing custom 404 error pages on my Windows/IIS/PHP webhost. Whenever
I'm trying to use the EntLib 3.1 within .net code for a dll which
Note : This question has been re-asked with a summary of all debugging attempts

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.