abigsmurf Posted June 27, 2008 Share Posted June 27, 2008 I've been tearing my hair out with this script and I simply can't tell what's wrong with my code. Basically I'm writing a script for a file explorer system that returns the last modified date of a folder (going through every subfolder within it to find the newest files). As there is an insane number of files (50,000 odd) in a folder the users will be browsing, it's requiring me to pre-process this to stop it being unbarably slow to use. A key part of this script is comparing the modified dates of folders to its subfolders and returning the newest date. To do this I got through a list of folders 1 by one and check to see if it's a subfolder of the one I'm trying to get the date for. However I'm having trouble with strpos simply never returning a true value. patharray is a 2d array where [0] is the url of a folder and [1] is the modified date (stored as a numerical timestamp). foreach( $patharray as $value) { $latestfile = 0; foreach($patharray as $value2) { $pos = strpos($value2[0], $value[0]); if ($pos === FALSE){} else{ print $value2[1]; if($latestfile < $value2[1]) { $latestfile = $value2[1]; } } } $value[1] = $latestfile; } the strpos bit just never seems to work and the else part of the following loop is never performed. I know the code is a bit horrible but can anyone offer any help on why it's not working? Quote Link to comment https://forums.phpfreaks.com/topic/112202-strpos-not-performing-as-expected/ Share on other sites More sharing options...
walovaton Posted June 27, 2008 Share Posted June 27, 2008 I'm not really sure but I guess there might be a problem when you loop the same array twice with foreach. foreach() always reset the internal array pointer so the outer foreach will clash with the inner foreach.... try replacing both foreachs with two for() loops. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/112202-strpos-not-performing-as-expected/#findComment-575955 Share on other sites More sharing options...
abigsmurf Posted June 27, 2008 Author Share Posted June 27, 2008 ah that could explain it, will try it out Quote Link to comment https://forums.phpfreaks.com/topic/112202-strpos-not-performing-as-expected/#findComment-575972 Share on other sites More sharing options...
sasa Posted June 27, 2008 Share Posted June 27, 2008 try <?php foreach( $patharray as $key => $value) { //$latestfile = 0; foreach($patharray as $value2) { $pos = strpos($value2[0], $value[0]); if ($pos === 0){ print $value2[1]; if($patharray[$key][1] < $value2[1]) { $patharray[$key][1] = $value2[1]; } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/112202-strpos-not-performing-as-expected/#findComment-576284 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.