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. Quote Link to comment 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); Quote Link to comment 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; Quote Link to comment 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; ?> Quote Link to comment 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; ?> Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
aQ Posted July 16, 2007 Author Share Posted July 16, 2007 Still does not display anything. Quote Link to comment 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. Quote Link to comment 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; ?> Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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); ?> Quote Link to comment 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! Quote Link to comment 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.