• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

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:
$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";

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. :)

  • 2
1 Solution
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.

mattias_sweAuthor Commented:
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. :)
mattias_sweAuthor Commented:
I have solved this through Workgroup Manager on the server. There I could add _www to the user group and everything just started to work. :)

In WGM under View you can mark that you want to see the system users. Change what directory you're in to Local.
Find World wide web user, click groups, change to LDAPv3 directory add the group you want from the OD to _www users groups. Save. Done.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now