Jump to content

Find date of last modified file on website?


HDFilmMaker2112

Recommended Posts

On a *nix host, you can exec this: 

 

find . -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort -r | head -n 1

 

Replace the "." with whatever the parent directory is of the tree you need to check.  I'd probably do something like this:

 

$lastchanged = `/usr/bin/find /path_to_search  -type f -printf '%TY-%Tm-%Td %TT %p\n' | /usr/bin/sort -r | /usr/bin/head -n 1`;

 

Keep in mind that this is going to scan the filesystem everytime it's called, so if this is a large tree, that could be some substantial IO.  The filesystem is not an indexed database.  The results you get are going to look like this:

 

2011-06-11 12:42:55.0000000000 /home/david/foo.txt

 

So you would have to break the string into pieces but that is something done easily with explode.

Link to comment
Share on other sites

I came up with this:

 

$files1 = glob("./ghosthuntersportal.com/*.php");
$files2 = glob("./ghosthuntersportal.com/includes/*.php");
$files3 = glob("./ghosthuntersportal.com/images/*");
$files=array_merge($files1, $files2, $files3);
if(is_array($files) && count($files) > 0)
{
    foreach($files as $file)
    {
     
$filetime=filemtime($file);
  
    }
}
$filetime=max($filetime);
$lastupdated="Last Updated: ".date ("F j, Y", filemtime($filetime));

 

But I get this error:

 

Warning: Wrong parameter count for max() in /home/zyquo/public_html/ghosthuntersportal.com/includes/func.php on line 32

 

It returns a date of Last Updated: December 31, 1969.

Link to comment
Share on other sites

On a *nix host, you can exec this: 

 

find . -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort -r | head -n 1

 

Replace the "." with whatever the parent directory is of the tree you need to check.  I'd probably do something like this:

 

$lastchanged = `/usr/bin/find /path_to_search  -type f -printf '%TY-%Tm-%Td %TT %p\n' | /usr/bin/sort -r | /usr/bin/head -n 1`;

 

Keep in mind that this is going to scan the filesystem everytime it's called, so if this is a large tree, that could be some substantial IO.  The filesystem is not an indexed database.  The results you get are going to look like this:

 

2011-06-11 12:42:55.0000000000 /home/david/foo.txt

 

So you would have to break the string into pieces but that is something done easily with explode.

 

I tried this just to check, error:

 

Warning: shell_exec() has been disabled for security reasons in /home/zyquo/public_html/ghosthuntersportal.com/includes/func.php on line 34

Link to comment
Share on other sites

You're sending a string to the max() function. When only one parameter is sent to max(), it must be an array. This is because of the way you're assigning the value: $filetime = should be filetime[] =.

 

Added the [] results in this:

 

Warning: max() [function.max]: Array must contain at least one element in /home/zyquo/public_html/ghosthuntersportal.com/includes/func.php on line 33

 

The $files Array seems to be blank.

Link to comment
Share on other sites

$files1[] = glob("./ghosthuntersportal.com/*.php");
$files2[] = glob("./ghosthuntersportal.com/includes/*.php");
$files3[] = glob("./ghosthuntersportal.com/images/*");
$files[]=array_merge($files1, $files2, $files3);
if(is_array($files) && count($files) > 0)
{
    foreach($files as $file)
    {
     
$filetime[]=filemtime($file);

    }
}
$filetime=max($filetime);
$lastupdated="Last Updated: ".date ("F j, Y", $filetime);

 

This returns the below:

 

Warning: filemtime() [function.filemtime]: stat failed for Array in /home/zyquo/public_html/ghosthuntersportal.com/includes/func.php on line 27

 

 

Link to comment
Share on other sites

my full path to the folder is public_html/ghosthuntersportal.com/sub-folders

 

the code is contained in func.php which is in the includes sub-folder.

 

If I do:

 

glob("*.php");

 

Wouldn't it assume I'm looking for php files in the sub-folder the script is contained in? and not the main folder.

Link to comment
Share on other sites

From your description, I think this is what you're after. Using a single dot signifies a path relative to the current directory, whereas a double dot indicates one directory above the current directory.

 

$files1[] = glob("../*.php");
$files2[] = glob("../includes/*.php");
$files3[] = glob("../images/*");

Link to comment
Share on other sites

From your description, I think this is what you're after. Using a single dot signifies a path relative to the current directory, whereas a double dot indicates one directory above the current directory.

 

$files1[] = glob("../*.php");
$files2[] = glob("../includes/*.php");
$files3[] = glob("../images/*");

 

Still no luck. Any other ways to accomplish this without glob()?

Link to comment
Share on other sites

Then let's try using that output to your advantage. This should work, assuming you've described the directory structure properly. See what happens with this code.

 

And why did you add square brackets to the $files1, $files2 and $files3 assignments?

 

$root = $_SERVER['DOCUMENT_ROOT'];

$files1 = glob("$root/*.php");
$files2 = glob("$root/includes/*.php");
$files3 = glob("$root/images/*");
$files =array_merge($files1, $files2, $files3);

if(is_array($files) && count($files) > 0) {
     foreach($files as $file) {
          $filetime[]=filemtime($file);
     }
}
$filetime=max($filetime);
$lastupdated="Last Updated: ".date ("F j, Y", $filetime);

Link to comment
Share on other sites

Then let's try using that output to your advantage. This should work, assuming you've described the directory structure properly. See what happens with this code.

 

And why did you add square brackets to the $files1, $files2 and $files3 assignments?

 

works perfectly like that... just removed the echos and now it's absolutely perfect. Thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.