Jump to content

strpos not performing as expected


abigsmurf

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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];
		}
	}
}
}
?>

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.