SaranacLake Posted December 23, 2019 Share Posted December 23, 2019 (edited) 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 December 23, 2019 by SaranacLake Quote Link to comment https://forums.phpfreaks.com/topic/309737-is_dir/ Share on other sites More sharing options...
requinix Posted December 23, 2019 Share Posted December 23, 2019 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. Quote Link to comment https://forums.phpfreaks.com/topic/309737-is_dir/#findComment-1572825 Share on other sites More sharing options...
SaranacLake Posted December 23, 2019 Author Share Posted December 23, 2019 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... Quote Link to comment https://forums.phpfreaks.com/topic/309737-is_dir/#findComment-1572826 Share on other sites More sharing options...
Psycho Posted December 23, 2019 Share Posted December 23, 2019 (edited) 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. filenamePath 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 December 23, 2019 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/309737-is_dir/#findComment-1572831 Share on other sites More sharing options...
SaranacLake Posted December 23, 2019 Author Share Posted December 23, 2019 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!! Quote Link to comment https://forums.phpfreaks.com/topic/309737-is_dir/#findComment-1572856 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.