Forum Moderators: coopster
$folders=array("folder1","folder2","folder3","folder4","folder5");
Only one of these folders will contain a file called data.csv and i want to know how i can get php to tell me which one but starting from folder3 onwards (skip folder 1 and 2)
can anyone please tell me how i can do this?
thanks
for($i=2;$i<count($folders);$i++) {
if(is_dir($folders[$i])) {
if($dh = opendir($folders[$i])) {
while(($file = readdir($dh)) !== false) {
if($file == 'data.csv') {
echo "Eureka! I found data.csv in {$folders[$i]}";
break;
}
}
closedir($dh);
}
}
}
You've got two loops and one break()...
(This can be made more efficient after testing.)
for($i=2;$i<count($folders);$i++) {
if(is_dir($folders[$i])) {
if($dh = opendir($folders[$i])) {
while(($file = readdir($dh)) !== false) {
if($file == 'data.csv') {
echo "Eureka! I found data.csv in {$folders[$i]}";
break;
}
}
closedir($dh);
[b]if($file == 'data.csv') {
echo "Eureka! I found data.csv in {$folders[$i]}";
break;
}[/b]
}
}
}
@ JC: How are you getting the spacing for code on the forum? I tried using the code formatting and (obviously) it didn't work... How are you indenting? I don't want to have to find it myself (gettin' lazy or something). lol.
for($i=3
and if i want it to check backwards from folder 5 i put:
for($i=4
is that correct?
if so then i cant seem to get it to work when it comes to folders lower than 4, so if i want it to check before folder 3 i would put
for($i=2
but that doesnt work
any ideas?