Link to home
Start Free TrialLog in
Avatar of Member_2_5175588
Member_2_5175588

asked on

How to let opendir read a directory in the Unix filesystem?

I am creating an web app that needs to read a directory and print what's in it.
That is not to hard in php, but my knowledge in unix permissions fails me.
Here is my code so far:
<?php
$link = "documents";
if(is_link($link)) {
      $dir = readlink($link);

      if($dh = opendir($dir)) {
            while(($file = readdir($dh)) !== false) {
                  echo "filename: $file -- filetype ".filetype($dir.$file)."\n";
            }
            closedir($dh);
      }
}

As you can see I am pointing to a folder via a sym link, I thought I could fool the system by doing that. ;)
I can't do opendir directly to the folder so I tried the sym link way instead.

The error I get is:
Warning: opendir(/Volumes/MirrorDrive/SharedItems/Documents/) [function.opendir]: failed to open dir: Permission denied in /Library/WebServer/Documents/documentserver/readdir.php on line 8

I guess this is a such noob question that most of you laugh at it.
But please give me a few minutes to answer my question. :)

TIA
Avatar of DrDamnit
DrDamnit
Flag of United States of America image

You can debug your code by doing chmod -R 777 * to basically let everything run and read there. I wouldn't leave it that way permanently, however. Not on a production box.

Once you know your code works, start backing down the permissions.

chmod -R 0755 * is what you should be shooting for.

You may also want to try to add the apache service user to your user group.

https://www.experts-exchange.com/questions/24981151/Easy-permissions-question.html
Avatar of Member_2_5175588
Member_2_5175588

ASKER

Well my script works with 777 so that is not the problem.
The problem is when I change to 755 it stops working so it is a permission problem.

I should also change my initial claim that I am running on a unix box, well I am sort of, it is an Apple Mac OS X Server 10.5.

What I am aiming for now is to add the user _www to the group that has access to this folder but that is easier said than done since all user/group management in done by the OpenDirectory database. So I need to fiddle with that through the CLI.
Not something I am comfortable with.

So if someone has input on that I am happy to receive that help. :)
ASKER CERTIFIED SOLUTION
Avatar of Member_2_5175588
Member_2_5175588

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial