This is part 6 and the last part of my series, PHP Directory Function Basics. When you have read names of items from a directory, you may want to know whether the item is a file or directory. The is_file() and is_dir() functions are used for that.
Note: If you cannot see the code or if you think anything is missing in this article (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Operational Method
With the is_file() and is_dir() functions, you do not need to open and close a directory or open and close a file. Each of these functions opens and closes its item.
The is_file() Function
The syntax of the is_file() function is,
bool is_file ( string $filename )
Here, the argument is the path and file name in a string. The function returns true if the filename exists and is a regular file, or false otherwise.
If you know the name of an item, e.g. fileA.txt, and its path e.g. c:/directory1/directory2/directory3/, then the following code will tell whether or not, fileA.txt is a regular file:
The is_dir() Function
The syntax of the is_dir() function is,
bool is_dir ( string $filename )
Here, the argument is a path ending with the directory of interest, in a string. The function returns true if the directory exists, or false otherwise.
If you know the name of an item, e.g. dirA, and its path e.g. c:/directory1/directory2/directory3/dirA, then the following code will tell whether or not, dirA is a directory:
List indicating Files and Directories
We have seen two ways of producing a list of items from a directory: one using the traditional method and the other using the scandir() function. When using either method, you can go on to check if the item is a regular file or a directory.
Imagine that you want to display the list of items in a browser and in front of each item you want to display the word, "file" or "directory" to indicate whether or not the item is a file or directory. You can use either the traditional method or the scandir() function in your code.
The scandir() function approach may take longer to execute, because a complete array will have to be produced first, from which you would then read each name before checking if it is a file or a directory. However, with the traditional approach, while the list is being created in memory, you can be reading the name and checking if the item is a file or directory. The traditional method also has the advantage that when you display its list in the order in which it is in memory, the files are separated from the directories. The scandir() function produces the list in alphabetical order mixing the files and directories (this is an advantage in other situations). The following code shows how you can solve this problem with the traditional method.
You can try this code, if you still have the directories and files created in part one of the series. In the while loop the is_file() and is_dir() fumctions are used to check if the item is a file or directory. The argument of the is_file() and is_dir() functions is the declared and assigned variable, $path. In the code, $path is a string, made up of the directory path and item name.
Let us end here. And we have come to the end of the series.
Conclusion
We treated the functions: opendir(), readdir(), closedir(), mkdir(), rmdir(), scandir(), is_file and is_dir. You should now be able to handle the basics of PHP directory functions. I wish you success in your undertakings.
Chrys
Published by Chrys Forcha
I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a... View profile
- Brain Based Vs. Traditional LearningThis is a proposal for a research report that is for a Brain Based Vs Traditional Learning research paper.
- Musicology and Semiotics: A Non-Traditional Approach to Studying Popular MusicThis article considers how musicology and semiotics can be combined to study popular music texts that represents and improvement over traditional musicological analysis.
How to Make a Traditional Thanksgiving Roast TurkeyNothing says Thanksgiving like a mouth watering turkey,roasted until golden brown. This traditional bird is easy to prepare and even easier to roast to perfection when you use a...- How to Block RIAA or MPAA IPsThis tutorial will show you how to install and configure Blocklist Manager. This is useful when you want to stay safe on the web or using P2P. It can also stop ads, government IPs and known bad IP ranges.
- 15 Smashing Online/Offline File Converters (Including Many Video Converters)Here goes a list of some cool file converters which could be used for the job. The list contains 4 online file converters (for any format), 8 online video converters, 1 offline file converter (for any format), 2 offli...
- PHP Regular Expressions
- PHP File Handling Basics
- Teaching Children to Read: Which Method is Best?
- Mediating Your Divorce Instead of Traditional Litigation
- Brain Based Learning Vs Traditional Learning
- Producing Web Pages of the Same Directory Automatically with PHP
- Dolphin: The Alternative (But Soon to Be Default) File Manager for KDE Linux



