Forum Moderators: coopster

Message Too Old, No Replies

creating a log directory using mkdir()

         

mgm_03

4:00 pm on Aug 26, 2005 (gmt 0)

10+ Year Member



I've got a cron script that runs each day and I need to capture any errors that occur into a log file. It works but I've decided to have the log files in subdirectories according to the date instead of all in just one folder. The script below should be making a directory in the format of: /path/to/logs/2005/08/ if it does not find that directory.

$year = date('Y');
$month = date('m');

$dir = dirname(__FILE__)."/";

$log_dir = $dir."logs/".$year."/".$month;

if (!is_dir($log_dir)) {
mkdir($log_dir, '0777');
}

$log_file = $log_dir."/log_".date("Y_m_d").".txt";

The message I'm receiving (edited for confidentiality):

Warning: mkdir(/path/to/logs/2005/08): No such file or directory in /path/to/wksht_cron2.php on line 36

jezra

4:22 pm on Aug 26, 2005 (gmt 0)

10+ Year Member



The first thing I would check for is the folder named after the year. If that folder doesn't exist, then the script will fail. Have the script check for a folder named after the year, if it doesn't exist, then create it. Now check for a folder named after the month. If the folder doesn't exist, create it. With both folders created, you should now be able to create your log file.

mgm_03

4:34 pm on Aug 26, 2005 (gmt 0)

10+ Year Member



Yeah, thanks. Someone else mentioned that in php4 directories are not created recursively but php5 will do so. will give it a try.

mgm_03

7:11 pm on Aug 26, 2005 (gmt 0)

10+ Year Member



Here's the error message:

Warning: mkdir(): SAFE MODE Restriction in effect. The script whose uid is 1047 is not allowed to access /webdocs/acme/public_html/admin/worksheets/logs/2005 owned by uid 65534 in /webdocs/acme/public_html/admin/worksheets/mkdir.php on line 33

Here's the code in mkdir.php ...it is a test script.


$year = date('Y');
$month = date('m');
$dir = dirname(__FILE__)."/";
$year_dir = $dir."logs/".$year."/" ;
$month_dir = $year_dir.$month."/";
if(!is_dir($year_dir)) {
mkdir($year_dir);
}
if (!is_dir($month_dir)) {
mkdir($month_dir);
}

$log_dir = $dir."logs/".$year."/".$month;

$log_file = $log_dir."/log_".date("Y_m_d").".txt";

dreamcatcher

7:29 pm on Aug 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are restrictions to using the mkdir function if your server is running in Safe Mode. Have you checked the PHP.net site for my info?

dc

jezra

7:52 pm on Aug 26, 2005 (gmt 0)

10+ Year Member



is cron calling your script directly or is cron telling the webserver to run the script? If cron is running the script through the webserver, then you should change the implementation of calling the script so that the script is called by cron directly, thereby having the script run by user root.

mgm_03

8:20 pm on Aug 26, 2005 (gmt 0)

10+ Year Member



Have you checked the PHP.net site for my info?" ....? I read the manual on mkdir() but didn't glean any insights. Looks like safe-mode limits the types of functions that are allowed.

is cron calling your script directly or is cron telling the webserver to run the script? If cron is running the script through the webserver, then you should change the implementation of calling the script so that the script is called by cron directly, thereby having the script run by user root.

...this is beyond my knowledge level of php. I wrote the script and told my hosting company I want it to run on the hour. That is all I know. However, I have encountered "uid" error messages.

I think for now, I will just manually create the 'month' directories... then when I have more time, move the logging to mysql for more flexibility. ....Thankyou for the assistance.

henry0

9:46 pm on Aug 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mgm_03,
jezra is on something, very possibly an ownership problem.
If you did not write the cron ask your ISP to set it as perjezra.

Quite often problems when creating files and dir are tied with ownership.