Jump to content

The fileatime( ) Function


iwato

Recommended Posts

The PROBLEM:According to W3Schools the following block of code is suppose to tell when the last time a particular file was accessed:

			<?php				clearstatcache();				$timestamp = fileatime("../include_and_require/footer.php");				echo "<br />";				echo "Last access: " . date("d M Y h:i:s.",$timestamp);				?>

Unfortunately, when I run the PHP script it produces the following: Last access: 01 Jan 1970 09:00:00. The PHP file renders accurately except for the date and time. My MAMP-Pro error log does reveal the following error, however: "[17-May-2010 12:48:23] PHP Warning: fileatime() [<a href='function.fileatime'>function.fileatime</a>]: stat failed for ../include_and_require/footer.php in /Applications/MAMP/htdocs/php-practice/functions/filesystem/fileatime/fileatime.php on line 37".Line 37 corresponds to the following PHP code line: $timestamp = fileatime("../include_and_require/footer.php"); BACKGROUND:Also, according to W3Schools, "Some Unix systems have access time updates disabled, because this function reduces performance for applications that regularly accesses a large number of files." Since Dreamweaver provides the last date of access of all the files that it manages surely this is not the source of the problem.Several weeks ago I zeroed out and rebuilt my entire machine. This is my first day back at PHP after a several week "Let's get acquainted period with TextWrangler" period. Well, I am now pretty well-acquainted and eager to return to PHP.All of the software that went into the creation of this file was freshly installed when I rebuilt my machine. Even my MAMP-Pro software appears to be running well now. However, I did change the factory settings for permissions. Of the three sets of permissions including owner, group, and other, I have squelched all permissions but for those related to the two owner/administrators of my machine. Have I blocked the system from doing its work? What could be the problem? This is not a fun way to restart with PHP . . . .Roddy

Link to comment
Share on other sites

Are you certain Dreamweaver does that? There is a difference between atime and mtime.
The same phenomenon occurs for both the fileatime( ) and filemtime( ) functions. I checked the filemtime( ) function just after posting about the fileatime( ) function for the same reason that you appear to be suggesting.Also, among my MacOS Finder View options both the date a file was created and last modified are available for all of my files. So what I see in the Dreamweaver File window is probably just a reflection of what can be seen in my Finder. In short, my access times have not been disabled within the UNIX framework.Roddy
Link to comment
Share on other sites

Thi could again be a permission issue. To read such attributes, PHP needs to have reading privilages. Being the owner of the file would probably also help.A stat may also fail if the file is locked. Try to run the PHP file without Dreamwaver running. Perhaps Dreamwaver is locking the file.

Link to comment
Share on other sites

Try to run the PHP file without Dreamwaver running. Perhaps Dreamwaver is locking the file.
Being the novice that I am I do not know how to run PHP files without Dreamweaver. When I go to the folder and try to open the file with Firefox or Safari while the webserver is turned on the PHP script fails to run at all. At least through Dreamweaver a date is returned.Roddy
Link to comment
Share on other sites

From Dreamwaver, open the file in a browser, and note the URL. Copy it. Close Dreamwaver. Lauch the browser and paste it back. For future's sake, you might want to bookmark the URL or something.

Link to comment
Share on other sites

From Dreamwaver, open the file in a browser, and note the URL. Copy it. Close Dreamwaver. Lauch the browser and paste it back. For future's sake, you might want to bookmark the URL or something.
I did as you suggested and can now clearly understand why my browser was unable to open the PHP file as part of my webserver. Thank you for the suggestion. I feel a little silly now for not having thought of it myself. In any case, Dreamweaver does not appear to be the source of the problem, as the resulting reported date was the same both with and without the application.By the way, I like the way you spell Dreamweaver. It does some nice things, but happy dreams are not one of them.Roddy
Link to comment
Share on other sites

See if the stat function works for you
Yes, it works. Please find below the contents of the data array generated by the stat( ) function.Curiously, the same content is reproduced twice (not shown) when echoed in a foreach loop.23488102669707633216150120018921274137385 [atime]1274066367 [mtime]1274066367 [ctime]4096Having followed your suggestion and shown to myself that the a-time differs from both the m- and c-times in the file I came to realize that the problem may lie elsewhere. A little further exploration revealed that I had mis-specified the location of the file to which the PHP script was pointing. When I adjusted the location, the script did as would be normally expected.Now, I am left with trying to understand . . .1) why the script ran at all, and more importantly why it generated the false date, and2) why the data array generated by the stat( ) function is echoed twice in a foreach loop.Maybe I should stop here and be thankful that my initial problem was not caused by mis-specified permissions in my MAMP-Pro interface. Then too, my goal is to understand PHP in my current environment, so that I can eventually make effective use of it. For the moment, it is like the ancient Greek Hydra (well this is one version of the story, anyway). You chop off one or more heads grow in its place.Thanks everyone for your great help!Roddy
Link to comment
Share on other sites

1. file*time() and stat() return false when there's an error. False is equivalent to an integer 0, and that, if read as a timestamp is 01 Jan 1970 09:00:00. In other words - it works because PHP is a loosely typed language - it automatically casts something if it's not in the right type.2. What's your full code again? The sample justomeguy gave shouldn't be looping.

Link to comment
Share on other sites

1) why the script ran at all, and more importantly why it generated the false date, and
It ran because when you give a nonexistent file to a function like fileatime the result is a warning, not a fatal error. Execution only stops on fatal errors, not warnings nor notices. Like boen pointed out, dates like these use Unix timestamps, which are just numbers. A Unix timestamp is the number of seconds since 1/1/70 00:00, so a value of 0 for a timestamp would convert to that time and date (depending on your time zone).
Link to comment
Share on other sites

What's your full code again? The sample justomeguy gave shouldn't be looping.
	<?php				print_r(stat("../fileatime/fileatime.php"));					?>		and later in the same file:		<?php		clearstatcache();				$statArray = stat("../fileatime/fileatime.php");		foreach ($statArray as $statArrayElement) {			echo $statArrayElement . "<br />";		}					?>

Roddy

Link to comment
Share on other sites

It ran because when you give a nonexistent file to a function like fileatime the result is a warning, not a fatal error. Execution only stops on fatal errors, not warnings nor notices.
Yes, my two important lessons from this topic besides paying more attention to filenames and directories are:1) Warnings and notices are not fatal, and 2) Return values can play deceptive tricks.
A Unix timestamp is the number of seconds since 1/1/70 00:00, so a value of 0 for a timestamp would convert to that time and date (depending on your time zone).
So, in this case 0 is 1/1/70. Understood.Many thanks to you and Boen Robot!Roddy
Link to comment
Share on other sites

2) why the data array generated by the stat( ) function is echoed twice in a foreach loop.
Check the manual for stat:http://www.php.net/manual/en/function.stat.phpDown in the return values section, you see a table that lists the array items. Notice that there is one column for the numeric index, and one for the associative. That means each piece of data is stored in the array in 2 places. If you defined the array yourself, it would look something like this:
$return = array(  0 => $device_number,  'dev' => $device_number,  1 => $inode_number,  'ino' => $inode_number,  2 => $inode_mode,  'mode' => $inode_mode, ...);

The reason they do that is so that you can access the array either numerically or by name. They also keep the numeric keys there for backwards compatibility, the names were added in PHP 4.0.6. So, if you use foreach to loop through it it's going to find each value in 2 places. You can see that if you print the key name also:

foreach ($statArray as $key => $val) {  echo '$statArray[' . $key . '] = "' . $val . '"<br>';}

Link to comment
Share on other sites

The reason they do that is so that you can access the array either numerically or by name. They also keep the numeric keys there for backwards compatibility, the names were added in PHP 4.0.6. So, if you use foreach to loop through it it's going to find each value in 2 places. You can see that if you print the key name also.
The following is a nifty piece of code. I did not know that what came after the as-word in the foreach loop expression was so flexible.
foreach ($statArray as $key => $val) {  echo '$statArray[' . $key . '] = "' . $val . '"<br>';}

My silly file error has turned into a treasure trove of new knowledge. The hydra has been killed. Well, at least until another appears . . . .W3Schools: My nomination for the internet's best teacher award!Roddy

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...