awohld has asked for the wisdom of the Perl Monks concerning the following question:

I have a directory "/upload" where files are uploaded and downloaded from the internet.

Here are my file permissions
[root@localhost upload]# ls -al total 56 drw-rw-rw- 3 wohldad nobody 4096 Aug 24 08:51 . drwxr-xr-x 26 root root 4096 Aug 22 00:41 .. -rw-r--r-- 1 nobody nobody 2318 Aug 24 08:50 1124891436 -rw-r--r-- 1 nobody nobody 14726 Aug 24 08:51 1124891462
The files I'm opening and downloading are 1124891436 and 1124891462. My web server is running as "nobody" and "nobody" has read and write permissions to the directory and files.

Now when I try to open the file to read it using:
open(DLFILE, "<$file_location/$id") || Error('open', 'file'); @fileholder = <DLFILE>; close (DLFILE) || Error ('close', 'file'); print "Content-Type:application/x-download\n"; print "Content-Disposition:attachment;filename=@filename[0]\n\n"; print @fileholder } sub Error { print "Content-type: text/html\n\n"; print "The server can't $_[0] the $_[1]: $! \n"; exit; }
I get an "The server can't open the file: Permission denied" error.

When I change the directory permissions to: 676, thus adding execute permissions for "nobody" I can download the file.

I read that "open" only needs read and write permissions on a file, is this true? Or do I also need "execute" permissions on a file to user perl's "open"?

How can I open a file without having to need execute permissions?

Replies are listed 'Best First'.
Re: Opening Files and File / Directory Permissions
by davidrw (Prior) on Aug 24, 2005 at 15:54 UTC
    You need to have executable permission on the directory, otherwise the user ('nobody') can't change into the directory to write stuff there (even if write permissions is set). This should fix it:
    chmod ug+x /your/webroot/path/upload chmod o-rw /your/webroot/path/upload # this is optional, but advi +sable
    Ultimately, needs to look like this (assuming both lines from above are run):
    [root@localhost upload]# ls -al total 56 drwxrwx--- 3 wohldad nobody 4096 Aug 24 08:51 . drwxr-xr-x 26 root root 4096 Aug 22 00:41 ..
Re: Opening Files and File / Directory Permissions
by bluto (Curate) on Aug 24, 2005 at 16:07 UTC
    In general under Unix, you must have at least write and execute permissions on the directory to create a file in that directory. Usually you also want read access on the directory as well, esp if you want to list it out.

    In fact, I'm surprised your 'ls -al' even listed any files at all since if you don't have execute permissions to the current directory you should not be able to list it (i.e. I can't get it to work on either AIX or Mac OSX). What OS are you on?

    You do not need to have execute permissions on the file itself to read or write it, just read or write permissions respectively.

      I was logged in a root when I did "ls -l", that's why it worked.
Re: Opening Files and File / Directory Permissions
by lidden (Curate) on Aug 24, 2005 at 15:54 UTC
    You need "execute" permissions to enter a directory, and you cant write in it without entering it.
Re: Opening Files and File / Directory Permissions
by merlyn (Sage) on Aug 24, 2005 at 15:43 UTC
    print "Content-Type:application/x-download\n"; print "Content-Disposition:attachment;filename=@filename[0]\n\n";
    You need spaces after your colons, and you really want to learn to write @filename[0] correctly as $filename[0].

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      merlyn,
      and you really want to learn to write @filename[0] correctly as $filename[0].

      Until of course you are coding in Perl6 in which case you want to go back to @filename[0]. This has the benefit of sigil consistency and wasn't done to confuse people despite what some people might think.

      Cheers - L~R

        Both Perl 1-5's method and Perl 6's method are consistent, just along different dimensions. In Classic Perl, $ means "one thing" and @ means "many things". In New Perl, $ means "scalar variable" and @ means "array variable".

        They both have advantages and disadvantages. For example, the rules about whether the index of an array is in scalar or list context were very very simple in Classic Perl (an element always provided scalar context to the index, and a slice always provided list context).

        In New Perl, it's become a complex issue with a lot of corner cases, because it depends a lot on how the index expression "looks". Ugh.

        The problem is that you're not "assigning" this value anywhere, so you don't get the contextual cues from the left side of such an assignment. And worse, how an index expression "looks" also affects whether it is describing an element or a slice, which then affects the rvalue if this is an assigment's lvalue. Double ugh. One little mistake will ripple forward badly.

        This is going to be hell to describe in the updated Llama. I'm not looking forward to writing that or teaching that.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.