Forum Moderators: coopster & phranque

Message Too Old, No Replies

Need short perl script to copy file...

Need file copy perl script.

         

bimjim

5:48 pm on Oct 17, 2000 (gmt 0)



Hi...

I have looked around the various archives but have been unable to find a perl script that would simply copy /abc/def/hello.html over to /ghi/jkl/hello.html, over-writing the old version of the file on simple cgi command from a browser.

The environment is a FrontPage web where I have allocated a friend access to a FP sub-web (but not the main web, Telnet or FTP, as I have clients in that space), and there is a SIG Discussion Forum I have set up for him in the main web he needs to update an info strip on without having access permissions.

I have instead set up a crontab to copy the file every night, but I see that as a temporary measure and I would like to make it possible for him to update the included file from a browser command line without having to ask me every time.

I have also looked at several other ways around it but none are practical for one reason or another - for instance, the Forum's index file is generated on the fly, so I can't just change it to an .shtml extension and insert SSI code.

Any ideas? I'm very html, perl and js literate, but please KISS (Keep It Simple, Sarge!)...

TIA.

Jim Lynch

Brett_Tabke

11:58 pm on Oct 17, 2000 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



#!/usr/bin/perl
# by brett tabke www.searchengineworld.com
# covered under the gnu...just take it...

#call from browser, www.domain.com/cgi-bin/mover.cgi?file=THESRC&dest=DEST
#where THESRC is the full path to the source file, and DEST is full path to the destination file.

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

&parseform;

$src = $FORM{'file'};
$destination = $FORM{'dest'};

print qq¦<li>src: $src¦;

if (-e $src) {
open(FILE,"$src");
flock(FILE,2) if $operatingsystem eq "unix";
@filedata = <FILE>;
chomp @filedata;
close(FILE);
}
else {
print "<b>*error*</b> source file not found: $src";
exit;
}

chomp @filedata;
$ls =@filedata;

print qq¦ lines: $ls <li>dest: $dest¦;

print "*warning* destination file exists - will be overwritten<br>" if -e $destination;

open(FILE,">$destination");
foreach $_ (@filedata) {
print FILE "$_\n";
}
close(FILE);

print "File copy appears to have been a big success.<br>" if -e $destination;

sub parseform {
#debug level >4 = print forms;

my ($buffer, @pairs, $name, $value);
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
if (length($buffer) < 5) {
$buffer = $ENV{QUERY_STRING};
}

@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/\r//g;
$FORM{$name} = $value;

if ($debug >4) {
print "<br>Form name : <b>$name</b> Value : <b> $value </b>\n";
}
}
}

bimjim

12:23 am on Oct 18, 2000 (gmt 0)



Maybe I'm having path problems... I tried setting the server root path (with ./usr/ etc.), the site root path (with /includes/ etc.) and even relative from the cgi-bin directory (with ../includes/ etc.).

Also tried leaving the filename off the destination path.

All efforts result in "src: *error* source file not found:".

Any ideas?

littleman

12:42 am on Oct 18, 2000 (gmt 0)



Try the full path, but without the '.' - so it be something like /full/file/path/file.txt.

littleman

12:48 am on Oct 18, 2000 (gmt 0)



If you can telnet in just to the folders you are working with and type 'pwd' at the command line to get your full path.

bimjim

1:12 am on Oct 18, 2000 (gmt 0)



Still nothing.

The file is at
9a/includes/discussionstrip.html

and needs to be copied to
x9a/includes/discussionstrip.html

The Telnet pwd path is
usr/www/users/candoo4u/9a/includes/discussionstrip.html
which is actually one of the paths I tried before (I use a diag.cgi script that passes me the server parameters).

I also just tried
usr/home/candoo4u/public_html/9a/includes/discussionstrip.html
which is really the virtual path, and - as expected - that did not work either.

Just in case you were wondering, the script is in fact running - it's returning the error message as coded - and I am transferring the file and its changes as ASCII, as well as chmod'ing it 755.

I also tried running it under both perl and perl5 (the server has both), same results.

The error is that the file is not found. It's there, I know that for sure from the directory listing and from putting the address in a browser, so path MUST be the problem.

Grrrrr.

Jim

bimjim

1:15 am on Oct 18, 2000 (gmt 0)



All paths shown above were inserted in the script with the fwd slash "/" at the beginning, BTW...

J

littleman

1:28 am on Oct 18, 2000 (gmt 0)



I just did a couple of test with this file setup:
?file=my/admin/test.txt&dest=my/admin/copy.txt
It worked fine. What about the permissions on the file or folder you are trying to copy from?

Also, try it as the full path but without the forward slash!

bimjim

1:41 am on Oct 18, 2000 (gmt 0)



read permission in the directory path is fine... 644

drwxr-xr-x 9a
drwxr-xr-x includes
-rw-r--r-- discussionstrip.html

Took the suggestion of dropping the leading slash - same thing. Even tried the path from the html root without the slash...

I can't even blame Good Ole Bill for this one... it's UNIX/Apache.

Here's the file - I just have to prove it really exists...
[candoo.com...]

Right now it's a pop-up in the Forum at
[candoo.com...]
(it's still blank because the file with the test message has not been transferred - that's what I'm still trying to do).

J

littleman

1:53 am on Oct 18, 2000 (gmt 0)



Ok, I'm at a loss.
If you have telnet access why don't you just use the unix command 'cp'
cp /usr/www/users/candoo4u/9a/includes/discussionstrip.html /where/you/want/it/to/go/discussionstrip.html
that should do it, I hope.

bimjim

2:16 am on Oct 18, 2000 (gmt 0)



I guess my provider has something preventing this... maybe even the FrontPage extensions are affecting the UNIX structure (the FP Include robot cannot import across webs/sub-webs within the same Domain, otherwise I would have done that a long time ago).

This I don't understand, because I have access to the entire Domain using WS_FTP, Telnet and the crontab... the perl script should also have universal access.

Ah, well...

Thanks, guys, anyway. I appreciate the help, and this is a great resource I just found.

Have a good one...

Jim

Brett_Tabke

1:17 pm on Oct 18, 2000 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I'd say it is because of permissions. If they are running a cgi wrapper, then everything must be set just right in order for something like this to work. Reread their documentation for the server setup.