Jump to content




[php]is_Dir


  • You cannot reply to this topic
4 replies to this topic

#1 svdragster

  • Members
  • 222 posts
  • LocationGermany

Posted 26 June 2013 - 12:08 PM

I guess I should post this here and not in Ask a Pro.

$allFiles = scandir($userDir);
	 for ($i = 1;$i < count($allFiles); $i++) {
	   //echo $allFiles[$i]."\n";
	   if (is_Dir($allFiles[$i])) {
		 echo "[$allFiles[$i] \n"]";
	   } else {
		 echo "Nope: $allFiles[$i] \n";
	   }
	 }
	 //print_r($allFiles);


I'm having trouble with this. It only prints .. when running this, even though there are 2 folders in the dir.
Can anyone help me?

#2 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 26 June 2013 - 01:06 PM

Hmm.. Im not that good at php, but I gave it a shot:

$allFiles = scan_dir( $userDir );
for( $i = 1; $i < count( $allFiles ); ++$i ){ // ++$i is actually more memory safe, but it really doesnt matter.
   if( is_dir($allFiles[$i - 1]) ){
     echo "Found Directory!:".$allFiles[$i - 1];
   } else {
     echo "Did not find directory :(/>";
   }
   echo "\n";
}
//print_r($allFiles);

The problem is that count actually returns all the indexes. But, you start with calling at 0! Thats the problem.
So your array looks like this:
$files = array(
   0 => "Some file name";
   1 => "Another file name";
);
But, count returns 2! So I have shown you a way around it.

If Im wrong in any way, please let me know. Then I want to learn that too, and remember, untested code ;)

#3 svdragster

  • Members
  • 222 posts
  • LocationGermany

Posted 26 June 2013 - 01:30 PM

[.]
[..]
Now it prints that without the echo "nope: $allFiles[$i]" :/

It somehow doesn't recognize the two folders as folders. It doesn't even work anywhere...

#4 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 26 June 2013 - 01:43 PM

Change the if statement in the for-loop to:
if( is_dir( $userDir."/".$allFiles[$i - 1] ){

I guess that would make it better

#5 svdragster

  • Members
  • 222 posts
  • LocationGermany

Posted 26 June 2013 - 01:45 PM

._. Thanks.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users