aQ Posted July 16, 2007 Share Posted July 16, 2007 Hello! I have a folder with a lot of .php files in it. They are named *somestuff*_*number*.php (blabla_10.php). I want to find the file with the highest value in its name. Is this possible? Thank you. Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/ Share on other sites More sharing options...
fanfavorite Posted July 16, 2007 Share Posted July 16, 2007 Something like this: $dir = $_SERVER['DOCUMENT_ROOT'] . '/phpfolder'; $dirHandle = opendir($dir); $highnumber = 0; while ($file = readdir($dirHandle)) { if(!is_dir($file)) { $num = substr(strrchr($file, "_"), 0); $number = $str_replace('.php','',$num); if ($highnumber < $number) { $highnumber = $number; } } } closedir($dirHandle); Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299794 Share on other sites More sharing options...
fanfavorite Posted July 16, 2007 Share Posted July 16, 2007 Sorry right under $highnumber = $number should be: $highpg = $file; Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299796 Share on other sites More sharing options...
aQ Posted July 16, 2007 Author Share Posted July 16, 2007 Thank you, but there is an error: Fatal error: Function name must be a string in /path/to/script.php on line 9 The script: <?php $dir = $_SERVER['DOCUMENT_ROOT'] . '/my/path/'; $dirHandle = opendir($dir); $highnumber = 0; while ($file = readdir($dirHandle)) { if(!is_dir($file)) { $num = substr(strrchr($file, "_"), 0); $number = $str_replace('.php','',$num); if ($highnumber < $number) { $highpg = $file; } } } closedir($dirHandle); print $highpg; ?> Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299800 Share on other sites More sharing options...
lightningstrike Posted July 16, 2007 Share Posted July 16, 2007 simply replace $str_replace with str_replace. <?php $dir = $_SERVER['DOCUMENT_ROOT'] . '/my/path/'; $dirHandle = opendir($dir); $highnumber = 0; while ($file = readdir($dirHandle)) { if(!is_dir($file)) { $num = substr(strrchr($file, "_"), 0); $number = str_replace('.php','',$num); if ($highnumber < $number) { $highpg = $file; } } } closedir($dirHandle); print $highpg; ?> Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299804 Share on other sites More sharing options...
aQ Posted July 16, 2007 Author Share Posted July 16, 2007 Thanks, no errors now, but no output either. Anyone? Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299809 Share on other sites More sharing options...
lightningstrike Posted July 16, 2007 Share Posted July 16, 2007 <?php $dir = $_SERVER['DOCUMENT_ROOT'] . '/my/path/'; $dirHandle = opendir($dir); $highnumber = 0; while (false !== ($file = readdir($dirHandle))) { if(is_dir($file)) { $num = substr(strrchr($file, "_"), 0); $number = str_replace('.php','',$num); if ($highnumber < $number) { $highpg = $file; } } } closedir($dirHandle); print $highpg; ?> I believe there was an accidental ! (not) operator placed in front of is_dir, try the new code. EDIT: changed the while loop to use false!== which is the php standard for readdir Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299818 Share on other sites More sharing options...
aQ Posted July 16, 2007 Author Share Posted July 16, 2007 Still does not display anything. Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299827 Share on other sites More sharing options...
lightningstrike Posted July 16, 2007 Share Posted July 16, 2007 <?php $dir = $_SERVER['DOCUMENT_ROOT'] . '/my/path/'; $dirHandle = opendir($dir); $highnumber = 0; while (false !== ($file = readdir($dirHandle))) { if(is_dir($file)) { $num = substr(strrchr($file, "_"), 0); $number = str_replace('.php','',$num); if ($highnumber < $number) { $highpg = $file; $highnumber = $number; } } } closedir($dirHandle); print $highpg; ?> That should most likely fix it. Assuming you have correctly entered your servers path in $dir variable. Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299830 Share on other sites More sharing options...
fanfavorite Posted July 16, 2007 Share Posted July 16, 2007 No !is_dir($file), means that it is NOT A DIRECTORY, which is the correct if character. Try taking the last "/" out of the $dir... so $_SERVER['DOCUMENT_ROOT'].'/my/path'. <?php $dir = $_SERVER['DOCUMENT_ROOT'] . '/my/path/'; $dirHandle = opendir($dir); $highnumber = 0; while ($file = readdir($dirHandle)) { if(is_dir($file)) { $num = substr(strrchr($file, "_"), 0); $number = str_replace('.php','',$num); if ($highnumber < $number) { $highpg = $file; } } } closedir($dirHandle); print $highpg; ?> Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299831 Share on other sites More sharing options...
aQ Posted July 16, 2007 Author Share Posted July 16, 2007 Still wont display anything. Thanks for trying guys. Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299835 Share on other sites More sharing options...
lightningstrike Posted July 16, 2007 Share Posted July 16, 2007 lol good point fanfavorite wasn't really looking carefully Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299837 Share on other sites More sharing options...
fanfavorite Posted July 16, 2007 Share Posted July 16, 2007 Ok, lets do some debugging. Add this $count variable <?php $count = 0; $dir = $_SERVER['DOCUMENT_ROOT'] . '/my/path/'; $dirHandle = opendir($dir); $highnumber = 0; while ($file = readdir($dirHandle)) { if(is_dir($file)) { $count++; $num = substr(strrchr($file, "_"), 0); $number = str_replace('.php','',$num); if ($highnumber < $number) { $highpg = $file; } } } closedir($dirHandle); print $highpg; print $count; ?> If $count is 0, then it is not getting into the if statement at all, which means your path to directory is wrong. If it is, it should be the number of files in that folder. Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299838 Share on other sites More sharing options...
aQ Posted July 16, 2007 Author Share Posted July 16, 2007 Count is 2, that's the correct number of files by now, but the number of the highest file is 3 Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299846 Share on other sites More sharing options...
fanfavorite Posted July 16, 2007 Share Posted July 16, 2007 After $number = str_replace('.php','',$num); echo $number; Make sure that it is displaying the number of the file correctly. Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299855 Share on other sites More sharing options...
aQ Posted July 16, 2007 Author Share Posted July 16, 2007 That displays nothing. So it seems like $number has no content? Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299859 Share on other sites More sharing options...
trq Posted July 16, 2007 Share Posted July 16, 2007 <?php $arr = glob('/path/to/files/*.php'); sort($arr); echo array_pop($arr); ?> Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-299892 Share on other sites More sharing options...
aQ Posted July 17, 2007 Author Share Posted July 17, 2007 That's great. I used str_replace to replace the path/to/file_ with "", and .php with "". Then, there was only a number left. That was exactly what I wanted. Thanks a lot! Link to comment https://forums.phpfreaks.com/topic/60274-solved-get-highest-file-name/#findComment-300276 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.