Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to save a daily log file in linux frpm a php script?

Posted on 2011-10-07
12
Medium Priority
?
437 Views
Last Modified: 2012-05-12
Im runing a script as a cron job every ten min. I want to create a daily log file for the results I thought its working but Im looking at monitor log and I only see one file for one date
why it is not creating a file for every day??


thank you!

$ourFileName = "/home/nurit/monitorTest/monitorLog"; 
	 $arg = (file_exists($ourFileName.date("Ymd")) == true ? "a+" : "w" );
	 $ourFileHandle = fopen($ourFileName, $arg) or die("can't open file");
	 $currDate = date("d/m/y : H:i:s", time());
	 $file_contents = "\nThe server is down for: ".$host." on D:".$currDate;
	 fwrite($ourFileHandle,$file_contents);
	 fclose($ourFileHandle);

Open in new window

0
Comment
Question by:Nura111
  • 9
  • 3
12 Comments
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36932901
Try this script and it may show you wants wrong

<?php
$ourFileName = "foo.log";
echo $ourFileName.date("Ymd")."\n";
$arg = (file_exists($ourFileName.date("Ymd")) == true ? "a+" : "w" );
echo $ourFileName.date("Ymd")."\n";
echo $arg."\n";
$ourFileHandle = fopen($ourFileName, $arg) or die("can't open file");
$currDate = date("d/m/y : H:i:s", time());
$file_contents = "\nThe server is down for: now on D:".$currDate."\n";
fwrite($ourFileHandle,$file_contents);
fclose($ourFileHandle);
?>

Open in new window


My modified script is checking for the file   foo.log20111007
but writes to  foo.log

Does it make sense now?
0
 

Author Comment

by:Nura111
ID: 36932917
not really... Do you know what wrong in my script?
0
 
LVL 13

Accepted Solution

by:
Hugh McCurdy earned 2000 total points
ID: 36932937
Here's my fixed code that I've tested.  This won't work for you as written.
<?php
$baseFileName = "log";
echo $baseFileName.date("Ymd")."\n";
$logFileName = $baseFileName . date("Ymd");
$arg = (file_exists($logFileName) == true ? "a+" : "w" );
echo $logFileName."\n";
echo $arg."\n";
$ourFileHandle = fopen($logFileName, $arg) or die("can't open file");
$currDate = date("d/m/y : H:i:s", time());
$file_contents = "\nThe server is down for: now on D:".$currDate."\n";
fwrite($ourFileHandle,$file_contents);
fclose($ourFileHandle);
?>

Open in new window

0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36932949
Yes, I know what's wrong with the script.  I'm going to try to fix in the next post and give it to you for testing.  Standby please.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36932985
I believe this will work.  I tested it best I can without actually being on your computer.

$ourFileName = "/home/nurit/monitorTest/monitorLog".date("Ymd");
         $arg = (file_exists($ourFileName) == true ? "a+" : "w" );
         $ourFileHandle = fopen($ourFileName, $arg) or die("can't open file");
         $currDate = date("d/m/y : H:i:s", time());
         $file_contents = "\nThe server is down for: ".$host." on D:".$currDate;
         fwrite($ourFileHandle,$file_contents);
         fclose($ourFileHandle);

Open in new window

0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36932994
You were testing against /home/nurit/monitorTest/monitorLog20111007 but then actually writing to /home/nurit/monitorTest/monitor

0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36933044
To be clear (I hope), I changed the first 2 lines.  The other lines remain the same.
0
 

Author Comment

by:Nura111
ID: 36933049
Do you mean to

You were testing against /home/nurit/monitorTest/monitorLog20111007 but then actually writing to /home/nurit/monitorTest/monitorLog ?

oh I get it cant believe it! :)
so by the way why every time its write to monitorLog its erased the dormer content isnt that suppose to add it on top of that?

Im trying really quick you suggestion to see if its work
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36933089
Yes, that's what I found.  It was overwriting.

Sometimes it helps just to have another set of eyes.


Note, it should be safe to always use mode 'a' to fopen even if the file doesn't exist.

http://php.net/manual/en/function.fopen.php

'a'       Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

For a freshly created file, the end of the file is also the beginning of the file.
0
 

Author Comment

by:Nura111
ID: 36933278
"so by the way why every time its write to monitorLog its erased the dormer content isnt that suppose to add it on top of that?"

So you think its because I opened it with a+ and not a?

0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36933361
Yes, since the file it was looking for never existed,    $arg = (file_exists($ourFileName.date("Ymd")) == true ? "a+" : "w" );     would always return 'w'

It never returned 'a+'


You don't need a+ since you aren't reading the file (right?).  A simple 'a' should do the trick.  But a+ isn't broken.
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36933525
Glad I could be of assistance.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Fine Tune your automatic Updates for Ubuntu / Debian
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question