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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI script not allowed to move files

Status
Not open for further replies.

MacTommy

Programmer
Feb 26, 2007
116
NL
I have a cgi script that tries to move files.

Code:
#!/usr/bin/perl -w

use strict;

use File::Copy;

print "content-type: text/html\n\n";

my $sFile = "myFile.txt";
my $sFolder = "myDir";

if( ! move($sFile, $sFolder) ) {
 	print "Couldn't move $sFile to $sFolder: $!<p>";
}
else {
 	print "Moved $sFile to $sFolder<p>";
}

If I run it from the command line it works perfectly, but if I load it in my browser it says:
Couldn't move myFile.txt to myFolder: Permission denied
Both in my Mac OS X system, bythe way, as on Windows XP.

If I look in my environment (%ENV) I see that there is no $ENV{USER} defined, so this strongly suggests this has to do with permissions (at least I would think)...

I can't imagine I am the first one to encounter this, but I can't find a lot about this topic here/on the net. But maybe that's just my lousy searching capabilities...
 
Hi

MacTommy said:
If I look in my environment (%ENV) I see that there is no $ENV{USER} defined, so this strongly suggests this has to do with permissions (at least I would think)...
Yes, probably. Although the missing environment variable should mean nothing. The usual way it to run web servers with no specified user, only group set to one specially created for the web server. And the group permissions should be enough to let the it access specific locations.

By the way, what web server it is ?

Feherke.
 
Thanks! It's an Apache server.

And I'm sorry, but what do you mean with 'group set to one specially created for the web server.'..??
 
Try using the full paths to the file and folder. As far as I know, XP should not restrict this action based on permissions. The error message is probably just very generic and has nothing to do with groups and such.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks Kevin!

I am using full paths however (I simplified the script somewhat for display purposes...).
 
Could you provide the full paths and the actual error message your getting?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Well the full paths differ on my Mac and on the Windows PC. Th e script works from the command line however, which leads me to think that the paths are ok.
The error message is the same in all cases:

Code:
Couldn't move myFile.txt to myFolder: Permission denied
 
running from command line in no way validates the way it will run from the web page.

For example if you have
my $sFile = "myFile.txt";
my $sFolder = "myDir";

and you run it from the directory that myFile.txt is in then yeah it might work great, but if you run the exact same script in the exact same location from the website it might return an error.

Why don't you try doing a
if (! -e $sFile) { print "Can't fine $sFile\n" }

or something just to make sure it really see's it.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
After taking a second look at your code, I think the problem is you are copying a file to a folder. You can't do that. You copy a file to another file.

The File::Copy module provides two basic functions, copy and move , which are useful for getting the contents of a file from one place to another.

The copy function takes two parameters: a file to copy from and a file to copy to. Either argument may be a string, a FileHandle reference or a FileHandle glob.

Maybe you want to use the move() function instead.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
He is using move()



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
ugh... sorry. I stayed up all last night (my son is not feeling well) and I've been on the go all day. I probably need to get some sleep.

Never mind my post! [yawn]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
That's 2 mistakes in one day buddy.. you've hit your limit. One more and I'll have to start docking your pay

[bigglasses]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
Well, yes. I have both a Windows PC and a PowerBook, and I happen to have Apache running on both. So I tested the script on both...

The script is working on Windows by the way, as KevinADC indicated it should above...
So, I am sorry about that! I must have made some other mistake somewhere.
The story is still the same for the Mac however...
 
Right, well I don't know much about OS X, but I believe it uses Unix-style file permissions - i.e. each file and directory has a set of permissions allocated to the owner, a group, and to everyone.

When you run a webserver, any scripts that it runs do not run with your user account, they run with the webserver's. If the server doesn't have permission to write to the directories concerned (it'll need to have write access to both the directory the file is moving from and the one it's moving to) it'll tank.

So to fix it, you'll either have to change permissions on the directory (fast-and-dirty approach would be to do a [tt]chmod 777[/tt] to give all users full access), or fine-tune the "user" that the webserver logs in as and/or grant them the necessary permissions.

You're probably best to pursue this in the OS X or Apache forums, as it isn't really a perl issue.

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
See the "Notes" section of the File::Copy documentation:





NOTES

*

On Mac OS (Classic), the path separator is ':', not '/', and the current directory is denoted as ':', not '.'. You should be careful about specifying relative pathnames. While a full path always begins with a volume name, a relative pathname should always begin with a ':'. If specifying a volume name only, a trailing ':' is required.

E.g.

copy("file1", "tmp"); # creates the file 'tmp' in the current directory
copy("file1", ":tmp:"); # creates :tmp:file1
copy("file1", ":tmp"); # same as above
copy("file1", "tmp"); # same as above, if 'tmp' is a directory (but don't do
# that, since it may cause confusion, see example #1)
copy("file1", "tmp:file1"); # error, since 'tmp:' is not a volume
copy("file1", ":tmp:file1"); # ok, partial path
copy("file1", "DataHD:"); # creates DataHD:file1

move("MacintoshHD:fileA", "DataHD:fileB"); # moves (don't copies) files from one
# volume to another





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Yes you're right. It probably is a permission thing, so it's not really a Perl issue anymore.
But thanks all the same!

And the colon as file separator is indeed a circumstance that is (soon to be) arcane. Goes back to the old Mac OS 9 days and before...
In MacOS X you are on a UNIX system, as far as these things are concerned.
 
Except he's not on OS (Classic), he's on OS X; and if the problem was the way he's naming directories it wouldn't work from the command line either.

Ahh, you're right. I need to pay more attention.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top