Jump to content

understanding a php code


hisoka

Recommended Posts

In this page of

 

I found this little PHP code . I tried to understand it but I could not grasp its meaning to 100%

 

So I will be grateful if you help me understand it :

<?php
$dir = "/images/";

// Open a directory, and read its contents
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "<br>";
    }
    closedir($dh);
  }
}
?>
$dir = "/images/";

is the directory that will be opened and read

if (is_dir($dir))

the if condition checks if the directory exists . Please if I am wrong correct me . Now comes the confusion and I have some questions

if ($dh = opendir($dir))

1) the opendir opens a directory handle . What is a handle ? why we need it ? and why we need it to open a directory ? why opendir does not just open a directory and that is all ? what is the difference between opening a directory and opening a directory handle ?

 

2)if the directory exists then open it . So why do not we put it like this :

if (is_dir($dir)){
  $dh = opendir($dir)

{...

why we use another if ? like in :

if (is_dir($dir)){
  if ($dh = opendir($dir)){

??

while (($file = readdir($dh)) !== false)

The readdir() function returns the name of the next entry in a directory. This is somehow confusing to me . So what does it mean

returns the name of the next entry in a directory

??

 

what does it mean

while (($file = readdir($dh)) !== false)

?? why false is used here in this context along with the !== ??

 

 

Link to comment
Share on other sites

By php.net definition opendir()

Opens up a directory handle to be used in subsequent closedir(), readdir(), and rewinddir() calls.

if (is_dir($dir)){//does directory exist
  if ($dh = opendir($dir)){// is directory handle achievable and not prevented because of directory permission restriction for instance. If not $dh = directory handle
while (($file = readdir($dh)) !== false)
//reads entries (files etc) from directory in filesystem order until false (no more entries)
Best to read description from php.net

 

http://php.net/manual/en/function.opendir.php

http://php.net/manual/en/function.readdir.php

  • Like 1
Link to comment
Share on other sites

the if condition checks if the directory exists . Please if I am wrong correct me .

Like the manual says, is_dir checks both that it exists and also that it is a directory and not a file.

 

the opendir opens a directory handle .

I disagree with the wording in the manual. I think it would be more accurate to say that it opens a directory and returns a handle to the open directory.

 

What is a handle ?

Just a resource that PHP uses internally. In this case it represents an open directory. Other handles might be to an open file or an open socket.

 

why we need it ?

Why would you not need it? If you have 1 script that opens 10 different directories or files and you want to do different things with each one, how are you supposed to tell PHP which one to use? That's what the handle is for.

 

why we use another if ?

Because good code should check for errors. Look at what the manual says:

 

Returns a directory handle resource on success, or FALSE on failure.

 

If path is not a valid directory or the directory can not be opened due to permission restrictions or filesystem errors, opendir() returns FALSE and generates a PHP error of level E_WARNING.

The readdir() function returns the name of the next entry in a directory. This is somehow confusing to me .

If the open directory contains several other directories and files then readdir will return each one in order. At a minimum it will always return 2 other directory references, even for a directory with nothing else in it. It will always return "." and ".." which represent the current directory and the parent directory.

 

?? why false is used here in this context along with the !== ??

Again, look at the manual:

 

Please note the fashion in which readdir()'s return value is checked in the examples below. We are explicitly testing whether the return value is identical to (equal to and of the same type as--see Comparison Operators for more information) FALSE since otherwise, any directory entry whose name evaluates to FALSE will stop the loop (e.g. a directory named "0").

One thing that the manual isn't clear on is that readdir will return false after it has read all of the items in a directory, not just on an error. So that loop ends after it reads everything.

  • Like 1
Link to comment
Share on other sites

I understand very well .

 

 

in order for an OS to deal with objects like windows , these objects should be identified . The OS cannot deal with objects if they are not identified to him . A handle is an object identifier or a pointer to that object . In the context of PHP a handle is an object identifier too like files handle , directory handles and so on . If I am wrong correct me

 

I think it would be more accurate to say that it opens a directory and returns a handle to the open directory.

 

 

why a handle to an open directory should be returned ? True can be returned if the directory is opened or false if it is not opened but why a handle ?

Link to comment
Share on other sites

That question was already answered

 

 

Why would you not need it? If you have 1 script that opens 10 different directories or files and you want to do different things with each one, how are you supposed to tell PHP which one to use? That's what the handle is for.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...