This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
svdragster's profile picture

[php]is_Dir

Started by svdragster, 26 June 2013 - 11:08 AM
svdragster #1
Posted 26 June 2013 - 01: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?
Engineer #2
Posted 26 June 2013 - 02: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 ;)/>
svdragster #3
Posted 26 June 2013 - 02: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…
Engineer #4
Posted 26 June 2013 - 02: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
svdragster #5
Posted 26 June 2013 - 02:45 PM
._. Thanks.