LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php and accessign local files (https://www.linuxquestions.org/questions/programming-9/php-and-accessign-local-files-4175460913/)

sniper8752 05-06-2013 02:00 PM

php and accessign local files
 
I am trying to use PHP to access local files.

I have the following code:

Code:

<?php

if ($handle = opendir('/var/www/users')) {
    echo "Directory handle: $handle\n";
    echo "Entries:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($entry = readdir($handle))) {
        echo "$entry\n";
    }

    closedir($handle);
}
?>

I get this:

Code:

Directory handle: Resource id #4 Entries: .. username .
What does that resource id #4 mean?

michaelk 05-06-2013 02:13 PM

It basically is the same thing as a file pointer. i.e. $handle = Resource id #4

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

sniper8752 05-06-2013 03:09 PM

does that mean it is just pointing to a file? not sure exactly what a file pointer is. why doesn't it just substitute it with the name of the file?

eSelix 05-06-2013 03:48 PM

Quote:

why doesn't it just substitute it with the name of the file?
You ask why converting "resource" to "string" not give you a filename? That has been programmed by PHP authors. I don't think it would be useful. You known what file/directory you opened (you just used it on the same line). On the other hand, you sometimes need to known this handle number.

If you ask why "opendir" command give you a "handle" and not a filename, because file names are inconvenient and unreliable in this situation. For example processes need to pass it to other processes. The string with file name and path can be long and every time it need to be copied byte by byte, first allocating appropriate memory for it. Better is using integer value, which coping is very, very fast for microprocessor.


All times are GMT -5. The time now is 02:45 PM.