Conditionals and PHP Directory Functions

PHP Directory Function Basics - Part 4

Chrys Forcha
Introduction
This is part 4 of my series, PHP Directory Function Basics. In this part of the series, we see what roles, conditionals play in PHP directory Functions.

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.

Return values of PHP Directory Functions
The PHP function opendir() should return a resource (handle). This is what we learned. That is fine. However, what about the case when the function fails to read the directory information from the disk. In such a case the function returns the Boolean value, false.

Now, know that the function, mkdir() returns the Boolean value, true, if it succeeds in making a directory and false if it fails in making the directory. Also know that the function, rmdir() returns the value, true if it succeeds in removing a directory and false if it fails.

The above three functions access the disk but areas (sectors) of the disk may not be in good condition all the time. So it is possible that any of the above three functions may return false at a particular time. There are other directory disk functions that behave in a similar way and would return false when there is trouble in the disk. There are other reasons why such a function can return false; however, I will not talk about such reasons.

When such a function returns false, it is good to inform the user of the problem in the disk. In the case of the opendir() function, you inform the user that the directory could not be read. In the case of the mkdir() function you inform the user that the directory could not be created. In the case of the rmdir() function, you inform the user that the directory could not be deleted.

Conditionals
Since you have to respect the situation when a function like one of the above returns false, it means in good programming, you should put such a function in an if-condition. If false is returned, an error message should be sent to the user. Let us look at examples.

The mkdir() Function
The following code shows how you can use the mkdir() function.

If you just want to create a directory and not do anything with it, then you can use the following code:

Here, there is negation of the condition. The expression in the condition of either of the two code samples is executed and the directory is created; true is returned just after the directory is created. In the second code sample, the true is negated to false and the if-block is not executed. If the directory is not created, false is retuned and negated to true, leading to the execution of the if-block, sending the error message. In the first code sample, there is no negation of the returned Boolean value; if it is true the if-block is executed; if it is false, the else-block is executed.

The rmdir() function
For similar reasons given above, the rmdir() function can be put in one of the following code samples:

The opendir() Function
In part 1 of the series, we used this function with the readdir() and closedir() functions, These three functions are usually used together. Now, this is the way you should write the code we saw in part 1.

The while loop here is the same as what we had in part 1. The closedir() function is now in the if-block. The corresponding else-block has the error message. I want you to note that the if-condition has the assignment operator (=) and not the comparison operator (==). This is an exceptional feature you have to accept, as that is what is required by the PHP specification.

Note that the while-loop is reading from memory and not from disk. The true value (Boolean value) in the while condition is just to make sure we read a list (next item after next item) of an unknown length to its end. It is not for error checking.

Wow, we are up and running. Everything is being unraveled.

Recommendation
For the use of error checking, always use the if-statement with an error message for the functions, mkdir(), rmdir(), opendir() and other functions that access the disk

Well, let us take a short break here and continue in the next part of the series.

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

To comment, please sign in to your Yahoo! account, or sign up for a new account.