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

Renaming file not working. 1

Status
Not open for further replies.

Aarem

Programmer
Oct 11, 2004
69
US
Hi. I have a script that takes a filename, checks to make it exists, and renames it to another name, if that name ends in the proper extension. Could someone please help me?





#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);

if ($ENV{'QUERY_STRING'}) {
@getpairs =split(/&/, $ENV{'QUERY_STRING'});
push(@pairs,@getpairs);
}
} else {
print "Content-type: text/html\n\n";
print "<P>Use Post or Get";
}

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

$value =~s/<!--(.|\n)*-->//g;

if ($formdata{$key}) {
$formdata{$key} .= ", $value";
} else {
$formdata{$key} = $value;
}
}
$method=$formdata{'method'};
$nm=$formdata{'filename'};
$user=$formdata{'user'};






if($method eq "rename")
{
$toName=$formdata{"rename-to"};

$allowed=0;

open (BUFFERING, ">>acceptable.txt");
@buffy=<BUFFERING>;
close BUFFERING;

foreach $type (@buffy)

{
if($toName =~ /\w{1,}\.$type/g)
{
$allowed=1;
}
}



if(-e "../albums/$user/$nm" && $allowed==1)
{
rename ("../albums/$user/$nm", "../albums/$user/$toName");
}
}






print "Location:album.cgi?id=$formdata{'user'}\n\n";



 
It it failing to see that the file exists of failing to rename the file?
 
Code:
open (BUFFERING, ">>acceptable.txt");
@buffy=<BUFFERING>;
close BUFFERING;

opening a file for append, and reading from end of file, I wouldn't expect @buffy to have any values

--Paul


Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
So, how should I define the file in open?

Code:
open (BUFFERING, "acceptable.txt");
or
Code:
open (BUFFERING, ">acceptable.txt");
?
 
If you are just opening the file to read from it, "acceptable.txt". ">acceptable.txt" would be used if you want to overwrite the data in that file with new data. ">>acceptable.txt" would be used if you want to add new data to the data thats already in the file, starting your add at the end of the file.

 
<acceptable.txt" would also work.

perldoc perlopentut
perldoc -f open


Good catch, Paul.

 
@mikevh,

pedanticism has its uses ;-)

--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
So...

Code:
#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);

    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
        @pairs = split(/&/, $ENV{'QUERY_STRING'});
    } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
        read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
        @pairs = split(/&/, $buffer);
        
        if ($ENV{'QUERY_STRING'}) {
            @getpairs =split(/&/, $ENV{'QUERY_STRING'});
            push(@pairs,@getpairs);
            }
    } else {
        print "Content-type: text/html\n\n";
        print "<P>Use Post or Get";
    }

    foreach $pair (@pairs) {
        ($key, $value) = split (/=/, $pair);
        $key =~ tr/+/ /;
        $key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    
        $value =~s/<!--(.|\n)*-->//g;
    
        if ($formdata{$key}) {
            $formdata{$key} .= ", $value";
        } else {
            $formdata{$key} = $value;
        }
    }
$method=$formdata{'method'};
$nm=$formdata{'filename'};
$user=$formdata{'user'};






if($method eq "rename")
{
$toName=$formdata{"rename-to"};

$allowed=0;

open (BUFFERING, "acceptable.txt");
@buffy=<BUFFERING>;
close BUFFERING;

foreach $type (@buffy)

{
@ta=split(/\n/,$type);
$type=@ta[0];
if($toName =~ /\w{1,}\.$type/g)
{
$allowed=1;
}
}



if(-e "../albums/$user/$nm" && $allowed==1)
{
rename ("../albums/$user/$nm", "../albums/$user/$toName");
}
}






print "Location:album.cgi?id=$formdata{'user'}\n\n";

That doesn't work, for some reason. Why?
 
Code:
#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);
use CGI;

$q=new CGI;
$method   = $q->param('method');
$nm       = $q->param('filename');
$user     = $q->param('user');
$toName   = $q->param('rename-to');
if($method eq "rename") {
  $allowed=0;
}
open (BUFFERING, "acceptable.txt");
while (<BUFFERING>) {
  $type=$_;
  if($toName =~ /\w{1,}\.$type/g) {
    $allowed=1;
  }
  if(-e "../albums/$user/$nm" && $allowed==1) {
    rename ("../albums/$user/$nm", "../albums/$user/$toName") or [b]die "Couldn't rename file"[/b];
  }
}
close BUFFERING;
open LOG, ">LOGFILE.TXT" or die "Cant open LOGFILE";
print LOG "\$method	==>$method\n";
print LOG "\$user  	==>$user\n";
print LOG "\$filename	==>$filename\n";
print LOG "\$rename-to	==>$rename-to\n";
close LOG;
print "Location:album.cgi?id=$q->param('user')\n\n";

This should do what I think you're intending to do, but will also give additional debug info

HTH
--Paul





Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
When I use that, no changes are made, and it takes me to album.cgi?id=CGI=HASH(0x804c7f4)->param('user')

I replaced
print "Location:album.cgi?id=$q->param('user')\n\n";

with

print "Location:album.cgi?id=" . $q->param('user') . "\n\n";

and that fixed it. Also, another problem is: LOGFILE.TXT reads this:

$method ==>rename
$user ==>carniojack424
$filename ==>
$rename-to ==>-to
 
I believe this would be the correct open script:



open LOG, ">LOGFILE.TXT" or die "Cant open LOGFILE";
print LOG "\$method ==>$method\n";
print LOG "\$user ==>$user\n";
print LOG "\$filename ==>$nm\n";
print LOG "\$rename-to ==>$toName\n";
close LOG;
 
progress ...

$filename is blank, but the big problem is $filename is blank, can you check that there's no mixed case in the html that's being served

Did the die command not fire on the rename ?? Is there no output in the webserver logs??

--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
All I get is a blank page. Is this the right kind of URL?


modFile.cgi?method=rename&rename-to=av.jpg&filename=avatar.jpg&user=carniojack424
 
Server's down. It'll have to wait 'till morning.
 
it says the right sort of things, you're not trying to do this over pocket IE by any chance ... just I noticed a few wobblies over the weekend. I'm sure it's because I didn't read the complete spec of something ...
--Paul
2morow then


Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top