Page 1 of 1

Opendir() and symbolic links on Linux

Posted: Mon Sep 06, 2010 7:55 pm
by r3nfr3w
Hi, I'm kind of new with PHP and I decided to code a web interface for my personnal media server at home. It is always nice to not absolutely having to be there when your wife wants to show some pictures from a trip to her mom.

For practical purpose, I don't want to keep all my pictures in my apache /var/www/ directory so I created a symlink to link on my other drive in the project folder. My problem comes when I try to read the symlink with opendir() from PHP. opendir() can't open it. I tested it with my "img" folder since it was a real one and it worked fine. I assume my code is good because of this little test and because most of it comes right from the PHP manual website. Some people say it might be the permissions but to be sure (and since it is only a home project), I decided to allow all permissions and it is not working either. I can assure you that my link isn't broken cause I can access it with bash. Is there a way to fix this problem? And it might be a stupid question I know, but since my knowledge in PHP is limited to the basics...

Here is my function if it can help:

Code: Select all

function getPicturesInList($server_path)
{
    // create an array to hold directory list
    $results = array();
    
    if ($handle = opendir($server_path))
    {
        /* This is the correct way to loop over the directory. */
        while (false !== ($file = readdir($handle)))
        { echo "<tr><td>". $file ."</td></tr>"; }

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

        closedir($handle);
     }
     else // This line is simply for a little test
     { echo '<script>alert("Cant open directory: '. $server_path .'")</script>'; }
     
    return $results;
}
If I'm not crazy, but it might be we never know, the problem really comes from the fact that opendir() don't want to open my symbolic link called...lets say...PicturesLink

Thanks in advance for any help.
R3nfr3w

Re: Opendir() and symbolic links on Linux

Posted: Mon Sep 06, 2010 8:47 pm
by requinix
/var/www/PicturesLink is actually a file - that's why it can't be opened as a directory. If you were to use a hardlink then this would work however there's really no need for that. Symlinks are much more common and, with a little modification to the code, still easy to read:

Code: Select all

function getPicturesInList($server_path)
{
    // create an array to hold directory list
    $results = array();
   
    while (is_link($server_path)) $server_path = readlink($server_path);
    if (!is_dir($server_path)) {
        // sanity check: not a directory
        trigger_error("'{$server_path}' is not a directory", E_USER_WARNING);
	return;
    }

    if ($handle = opendir($server_path))
    {
        // ...

Re: Opendir() and symbolic links on Linux

Posted: Mon Sep 06, 2010 10:24 pm
by r3nfr3w
First of all, thanks for the fast reply!

Yes, you are right and I should have thought of it. It is considered has a filename and not a directory.

Your piece of code is simple and effective but still it fails your sanity check. The problem, $server_path (after reading the link) is not considered as a directory nor a filename. Kind of weird since I printed it in my browser and it is the real path of my symlink. Again, I verified the permissions and everything seems fine. Could it be possible the problem comes from my apache configuration? It is still the default one from the installation.

Thanks in advance,
R3nfr3w

Re: Opendir() and symbolic links on Linux

Posted: Thu Aug 04, 2011 6:20 am
by warwickhunt
try using scandir() instead of opendir()

I had the same issue and this fixed it!

;-)