HDFilmMaker2112 Posted June 11, 2011 Share Posted June 11, 2011 Is there anyway to write a script that will find the file with the most recent file modified/uploaded date and return it? I know I can use filemtime() to return the time from the specific file, but I need a way to find the last modified file. Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/ Share on other sites More sharing options...
HDFilmMaker2112 Posted June 11, 2011 Author Share Posted June 11, 2011 Find the glob() function, but don't know if it's what I'm looking for... Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228409 Share on other sites More sharing options...
gizmola Posted June 11, 2011 Share Posted June 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228411 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 11, 2011 Author Share Posted June 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228416 Share on other sites More sharing options...
Pikachu2000 Posted June 11, 2011 Share Posted June 11, 2011 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[] =. Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228419 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 11, 2011 Author Share Posted June 11, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228420 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 11, 2011 Author Share Posted June 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228421 Share on other sites More sharing options...
Pikachu2000 Posted June 11, 2011 Share Posted June 11, 2011 Post the current code . . . Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228430 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 11, 2011 Author Share Posted June 11, 2011 $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 Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228432 Share on other sites More sharing options...
Pikachu2000 Posted June 11, 2011 Share Posted June 11, 2011 glob takes a filesystem path. Do you actually have a directory named "ghosthuntersportal.com"? Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228434 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 11, 2011 Author Share Posted June 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228435 Share on other sites More sharing options...
Pikachu2000 Posted June 11, 2011 Share Posted June 11, 2011 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/*"); Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228462 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 11, 2011 Author Share Posted June 11, 2011 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()? Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228495 Share on other sites More sharing options...
Pikachu2000 Posted June 11, 2011 Share Posted June 11, 2011 It works with glob, I've tested it locally. The issue here is your path, most likely. What is the output from this? echo $_SERVER['DOCUMENT_ROOT']; Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228499 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 11, 2011 Author Share Posted June 11, 2011 It displays this: /home/zyquo/public_html/ghosthuntersportal.com Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228521 Share on other sites More sharing options...
Pikachu2000 Posted June 12, 2011 Share Posted June 12, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228548 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 12, 2011 Author Share Posted June 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/239089-find-date-of-last-modified-file-on-website/#findComment-1228551 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.