Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Permissions via Script

Status
Not open for further replies.

rhnewfie

Programmer
Jun 14, 2001
267
CA
I have a script that receives input from a web form and writes it to a flat file on the server. If the file is not already present the script creates it but then the permissions on the file will not let information be written to the file. Is there a way that I can change the permissions on the file when the script creates it so that it is read/write/exe??

Thanks
RHNewfie There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
Have you looked at umask() and chmod()?

I have a factory settings function, that I can use remotely to ensure the user writeable directories exist:

sub factorysettings {
# prime the mask for creation
umask 002;

# directories user can access
my @accessallareas = (
"$cachedir",
"$downloaddir",
"$intradir/tmp",
"$homedir/generated",
"$homedir/resourcefiles",
);

# create any directories that don't exist
foreach my $dir (@accessallareas) {
mkdir $dir, 0777 if(!-e $dir);
}

# ensure they have correct permissions
chmod 0777, @accessallareas;
}

instead of directories you can substitute for files.

HTH,
Barbie. Leader of Birmingham Perl Mongers
 
This is how I am trying to do it now:

if (-f $file)
{

open(OUTPUT, ">>$file") ¦¦ die "can't open $file";
print "Content-type: text/plain\n\n ";
print......
}
else
{
open(OUTPUT, ">>$file") ¦¦ die "can't open $file";
chmod 0777, $file;
print "Content-type: text/plain\n\n ";
$file is previously defined as the absolute path to the file There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
Anyone? There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
Is this correct???

if (-f $file)
{
umask 002;
open(OUTPUT, ">>$file") ¦¦ die "can't open $file";
chmod 0777, $file;
print "Content-type: text/plain\n\n ";
print OUTPUT RPad("$contents{'firstname'}",30);
print OUTPUT RPad("$contents{'lastname'}",30);
print OUTPUT RPad("$contents{'streetaddress'}",30);
print OUTPUT RPad("$contents{'unit'}",5);
print OUTPUT RPad("$contents{'city'}",30);
print OUTPUT RPad("$contents{'postalcode'}",10);
print OUTPUT RPad("$contents{'province'}",30);
print OUTPUT RPad("$contents{'email'}",30);
print OUTPUT RPad("$contents{'emailtype'}",4);
print OUTPUT RPad("$contents{'distance'}",5);
print OUTPUT "$contents{'location'}\n";
close (OUTPUT);
}
else
{
umask 002;
open(OUTPUT, ">>$file") ¦¦ die "can't open $file";
chmod 0777, $file;
print "Content-type: text/plain\n\n ";
print OUTPUT RPad("$contents{'firstname'}",30);
print OUTPUT RPad("$contents{'lastname'}",30);
print OUTPUT RPad("$contents{'streetaddress'}",30);
print OUTPUT RPad("$contents{'unit'}",5);
print OUTPUT RPad("$contents{'city'}",30);
print OUTPUT RPad("$contents{'postalcode'}",10);
print OUTPUT RPad("$contents{'province'}",30);
print OUTPUT RPad("$contents{'email'}",30);
print OUTPUT RPad("$contents{'emailtype'}",4);
print OUTPUT RPad("$contents{'distance'}",5);
print OUTPUT "$contents{'location'}\n";
close (OUTPUT);
} There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
Great thanks a lot! There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
The script almost works as above, the only problem is that it will not write the information to the file after it creates it for some reason. I've had it looked at by the unix admin and he can't see why it doesn't work. Has anyone out there had a problem like this??? >:-< There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
If the file is being created what permissions has it been created with, and who is the owner/group set to? If the file isn't being created check that the directory has write access for the webuser too.

Are you running this under a *nix or Win32 system?

Barbie. Leader of Birmingham Perl Mongers
 
missbarbell

It is running on a *nix system and the permissions on the file after it has been created are: -rw-rw-rw-

However it still won't allow me to write to it. Once it did but it wasn't until after about an hour or so but nothing had changed in the script.

Thnaks
RHNewfie There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
That is very strange. With a write-all setting, there should be no problem. I think this is the sort of thing that needs a handson examination to figure it out. If you do discover the problem let us know.

Barbie. Leader of Birmingham Perl Mongers
 
It seems that if the file gets put on the server with the server id then you can't write to it, if the admin puts a copy of the file there with my id then you can, if I put the file there with my id then you can't. It's really wierd!!! There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
Can I do a chown in my perl script?

RHNewfie There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
Yes. But the user profile running the script must have access to do that. You may suffer the same fate as when writing to the file.

HTH,
Barbie. Leader of Birmingham Perl Mongers
 
hmm, can you give me a hint about how to do it. There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
Is that all the code?
Are you coming back and hitting this file again later in the code and accidentally overwriting it with another open statement?
Or, is there another web process that may be overwriting the file?

I don't see how, if the web daemon can create the file, that it would not then have sufficient permissions to write to the file. There must be some else going on.
.... seems to me, anyway.... :cool:

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
That's pretty much all of the code. You raise an interesting point though. If the file already exists and the code heads in this direction:

if (-f $file)
{
umask 002;
open(OUTPUT, &quot;>>$file&quot;) ¦¦ die &quot;can't open $file&quot;;
chmod 0666, $file;

etc...

would this be overwriting the file? Perhaps I should be opening the file another way if it already exists, however I haven't seen one (I'm new to perl). Then, in the else condition, should I be creating the file with some other method and then using open(OUTPUT, &quot;>>$file&quot;) to open it? Have you got any ideas? X-) There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top