...

View Full Version : which path for filemtime to check whole directory?



cfructose
07-31-2007, 07:38 PM
Hi,

I'm using this code to display the last time any page of my site was updated:


putenv("TZ=America/Los_Angeles");
echo "Site last updated: ".date("dS F Y h:i A", filemtime("/path/to/directory/."))." (PST).";

...but I'm not sure what to change the /path/to/directory/ to...

Is it the path as I see it in my FTP - that doesn't seem to work?
What does leaving a mere dot there mean?

Does
filemtime(".") mean 'everything in the current directory'?

Thanks

PappaJohn
07-31-2007, 08:38 PM
If it's in the current directory, just use the filename eg: 'somefile.txt'

cfructose
07-31-2007, 08:55 PM
Thanks, but perhaps I didn't explain clearly...

What I mean is that I want to check when ANY file on the whole site was last edited/updated/uploaded.

PappaJohn
07-31-2007, 09:23 PM
Ah. Well filemtime() will check a single file. To use it the way you want, you'll have to loop through each of the files in the directory.

If you wanted to only check a subset of the files in the directory, you could populate an array with the filenames and loop through that.

cfructose
08-01-2007, 02:01 AM
Erm, I'm confused... While I'm sure you're right, it seems that the following quote contradicts you. (This is taken from: http://de3.php.net/function.filemtime )


To get the last modification time of a directory, you can use this:


$getLastModDir = filemtime("/path/to/directory/.");

Take note on the last dot which is needed to see the directory as a file and to actually get a last modification date of it.

This comes in handy when you want just one 'last updated' message on the frontpage of your website and still taking all files of your website into account.

Am I missing something?
Thanks a lot. :-)

_Aerospace_Eng_
08-01-2007, 02:06 AM
To show the last modified date of the file that code is on you can use $_SERVER['SCRIPT_FILENAME'];
which will show use the script filename that the echo is running on.

While this information may or may not be what you need it might be useful to someone.

PappaJohn
08-01-2007, 02:11 AM
Am I missing something?
Thanks a lot. :-)
Hmm. Wasn't aware that would work (obviously I've never tried it).

In that case, I'd try:

filemtime("/.")

cfructose
08-01-2007, 02:21 AM
Thanks _Aerospace_Eng_ ... useful for some other occasion! :-)

PappaJohn - I tried using /. but the date then shown is certainly much earlier than the last site update.

Of course, the guy who posted the quote I gave in last post may just be wrong(?)

OK, I'm trying to understand how to indicate paths: what does the full stop mean? And does a / with nothing before it indicate the root directory?

Regardless, to return to my problem, I'm simply looking for a way to print a "Site last updated on..." message in the footer of the site that will automatically detect the last time I uploaded any new/edited file.
Is the only way really to loop round an array of every file, checking each one's timestamp?

Thanks

_Aerospace_Eng_
08-01-2007, 03:12 AM
This should work

putenv("TZ=America/Los_Angeles");
echo "Site last updated: ".date("dS F Y h:i A", filemtime($_SERVER['DOCUMENT_ROOT']."/."))." (PST).";
What the $_SERVER['DOCUMENT_ROOT'] should return is something like this

/home/username/public_html

cfructose
08-01-2007, 11:27 PM
Almost there... Thanks a lot!

It successfully updates the date and time for any edited and re-uploaded files in the root directory.

The final step (I'm hoping this is just a small alteration neede?), is for it to recognise all subfolders too.

To what should I change:


filemtime($_SERVER['DOCUMENT_ROOT']."/.")

My experiments are all failing! :-)

_Aerospace_Eng_
08-02-2007, 02:28 AM
What if you did just this?

filemtime($_SERVER['DOCUMENT_ROOT'].'/')

cfructose
08-02-2007, 11:21 AM
No, that's still only reflecting the last change to a file in the root directory, but not in all subfolders.

I ask again: I'm trying to understand how to indicate paths: what does the full stop mean? And does a / with nothing before it indicate the root directory?

I can't seem to find anything out about this, as I'm not sure what to Google for the info. "path syntax?" I'm guessing in the dark without that knowledge...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum