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

Posted on 2009-12-16
Last Modified: 2013-12-20
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. :)

Question by:mattias_swe
    LVL 32

    Expert Comment

    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.

    Author Comment

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

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now