I am trying to ensure that only files of a certain type can be uploaded.
Why doesn't this work??
(It's a code snippet..not the whole file)
[code=perl]
if(my $file = $q->param('avatar' )){
$CGI::POST_MAX = 1024 * 39; # Limit to 39 kb.
my $dir = '../img/avatars';
my $charsok = 'a-zA-Z0-9_.-';
my ($filename,unde f,$ext) = fileparse($file ,qr{\..*});
if($ext !~ /jpeg|gif|png/gi){
$emess .= qq* <li>That is an invalid file, avatars MUST be in "jpeg", "gif" or "png" format.</li> *;
}
else{
$filename .= $ext;
$filename =~ tr/ /_/;
$filename =~ s/[^$charsok]//g;
if($filename =~ /^([$charsok]+)$/){
$filename = $1;
my $upload_file = $q->upload('avatar ');
open(FILE, ">$dir/$filename") || Tools->listErr( $! );
binmode FILE;
while (<$upload_file> ){
print FILE;
}
close(FILE);
$sth = $dbh->prepare(qq~ UPDATE users SET avatar=? WHERE id=? ~);
$sth->execute( "$dir/$filename", $user->{id}) || Tools->listErr( $sth->errstr );
$sth->finish;
}
else{
$emess .= "<li>Filena me not valid, may only contain these characters: $charsok</li>";
}
}
}
[/code]
Anyone know why the $ext variable isn't matching?
I can upload a .exe and it won't skip a beat....
NOT good.
Why doesn't this work??
(It's a code snippet..not the whole file)
[code=perl]
if(my $file = $q->param('avatar' )){
$CGI::POST_MAX = 1024 * 39; # Limit to 39 kb.
my $dir = '../img/avatars';
my $charsok = 'a-zA-Z0-9_.-';
my ($filename,unde f,$ext) = fileparse($file ,qr{\..*});
if($ext !~ /jpeg|gif|png/gi){
$emess .= qq* <li>That is an invalid file, avatars MUST be in "jpeg", "gif" or "png" format.</li> *;
}
else{
$filename .= $ext;
$filename =~ tr/ /_/;
$filename =~ s/[^$charsok]//g;
if($filename =~ /^([$charsok]+)$/){
$filename = $1;
my $upload_file = $q->upload('avatar ');
open(FILE, ">$dir/$filename") || Tools->listErr( $! );
binmode FILE;
while (<$upload_file> ){
print FILE;
}
close(FILE);
$sth = $dbh->prepare(qq~ UPDATE users SET avatar=? WHERE id=? ~);
$sth->execute( "$dir/$filename", $user->{id}) || Tools->listErr( $sth->errstr );
$sth->finish;
}
else{
$emess .= "<li>Filena me not valid, may only contain these characters: $charsok</li>";
}
}
}
[/code]
Anyone know why the $ext variable isn't matching?
I can upload a .exe and it won't skip a beat....
NOT good.
Comment