Jump to content

is_dir()


SaranacLake

Recommended Posts

I have the following code...

	While(($file = readdir($handle)) !== FALSE){
	   if(is_dir($file))){
	      echo "$file - Directory<br>";
	   }else{
	      echo "$file - Not Directory<br>";
       }
    }

 

And I have the following files/folders...

	2019-holiday-party
	     _photos
	     _thumbnails
	     xxx
	     IMG_2205.png
	

 

I get these results...

	. - Directory
	.. - Directory
	_photos - Not Directory
	_thumbnails - Not Directory
	IMG_2205.png - Not Directory
	xxx - Not Directory
	

 

Why do the directories "_photos" and "_thumbnails" and "xxx" get reported as "Not Directory"???

 

Also, where does the "." (dot) and ".." (dot dot) come from??

 

Edited by SaranacLake
Link to comment
Share on other sites

If you really don't know what . and .. are then you need to research the very basics of how every single filesystem in existence works.

$file is not a full path to the file. It's just the name of the file. So the question is whether you think is_dir("_photos") should say true or false.

Link to comment
Share on other sites

6 minutes ago, requinix said:

If you really don't know what . and .. are then you need to research the very basics of how every single filesystem in existence works.

$file is not a full path to the file. It's just the name of the file. So the question is whether you think is_dir("_photos") should say true or false.

I understand that "." is current directory and ".." is one directory up, but I still don't see why my code includes the same directory (recursive) or the directory above it?!

 

"_photos" and "_thumbnails" and "xxx" are folders in the parent directory, so YES, I expected my code to flag them as such...

Link to comment
Share on other sites

8 hours ago, SaranacLake said:

I understand that "." is current directory and ".." is one directory up, but I still don't see why my code includes the same directory (recursive) or the directory above it?!

 

"_photos" and "_thumbnails" and "xxx" are folders in the parent directory, so YES, I expected my code to flag them as such...

readdir() returns all the "items" in a directory - that includes the '.' and '..', because that is how the file system works. As to your second comment, I think you did not understand @requinix's response. From the 1st line of the description in the manual for readdir()

Quote

Returns the name of the next entry in the directory.

It just returns a string value for the name of the entity. Then, if you look at the manual for is_dir() it states

Quote

is_dir ( string $filename ) : bool

Tells whether the given filename is a directory.

filename
Path to the file. If filename is a relative filename, it will be checked relative to the current working directory. If filename is a symbolic or hard link then the link will be resolved and checked. If you have enabled safe mode, or open_basedir further restrictions may apply.

Is it clear now? You will need to provide the FULL PATH to the file/directory name that is returned from readdir(). Alternatively, you could use glob() instead of readdir and it will return the full path. Not seeing the code to create $handle, I assume you are providing a path to the directory. You would need to append that to the beginning of the file name in order to correctly test using is_dir. However, I think glob() would be a better solution for what you need.

Edited by Psycho
Link to comment
Share on other sites

7 hours ago, Psycho said:

readdir() returns all the "items" in a directory - that includes the '.' and '..', because that is how the file system works. As to your second comment, I think you did not understand @requinix's response. From the 1st line of the description in the manual for readdir()

It just returns a string value for the name of the entity. Then, if you look at the manual for is_dir() it states

I got it working now - thanks for the clarification!!

 

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.