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

I cant figure out how to get perl to do this I am making a directory and then putting a file in it. I can do that due to much help from a few of you.

But now when I try to make the files and the directories world readable ( because it is for the web ) and let the program write the redirect file this happens
Content-type: text/html Software error: Permission denied at validate_school_data.cgi line 307. For help, please send mail to the webmaster


am I chmod-ing the files and directories to the wrong thing. And if so what should it be set to and is there data on this elsewhere? Below is the culprit code Ln 307 is where it tries to write the redirect file.
#take school name and replace spaces with underlines $dir =~ s/ /_/g; mkdir( "$topdir/$dir" ,0 ) or die "Couldn't mkdir $topdir/$dir: +$!"; $mode = 0755; chmod( $mode, '$topdir/$dir'); #create redirect page for that directory my $outputpage; open(TEMPLATE, "default_redirect.html") or die "can't open redirect page"; local($/) = undef; $outputpage = <TEMPLATE>; close(TEMPLATE); $outputpage =~ s/\$name/$name/g; open( REDIRECT, ">$topdir/$dir/index.html" ) or die "$!"; print REDIRECT $outputpage; close REDIRECT; chmod $mode, '$topdir/$dir/index.html';

Replies are listed 'Best First'.
Re: File permissions problems
by djantzen (Priest) on Nov 01, 2002 at 01:43 UTC

    I believe the problem is in chmod($mode, '$topdir/$dir'). Because the directory name is single-quoted, those variables are not interpolated, e.g., their real values are not substituted into the string. Instead, they are treated as literal strings and therefore you're actually trying to set the permissions on a directory named '$topdir/$dir', not the one you want. Then later, when you try to write a file into your target directory, you'll get a 'permission denied' because that directory has not been made user writeable. Try double-quoting the variables constituting the directory name, as in chmod($mode, "$topdir/$dir").

    You can also check the value of $! after a chmod call to see what happened. In this case warn $! if $! yields 'No such file or directory'.

      it is also possible that you have a index.html already in the directory which does not have write permissions on it.