fortwilliam
Programmer
Hi, I am very new to "object oriented programming". I have this script which I have been using for a while to allow people to upload files to a website. Now I am trying to adapt the same script to upload files to multiple websites specified in an array. This is for a content management system for our websites. I thought I could just stick a foreach loop round most of the script and that would work. However, no matter what I do, I get one file uploaded and the next file is created but has no content. I have been trying to upload image files but the script should work with all files as required, including .pl perl files. Just to make it clear, I get the correct image file in all websites but only the first one in the @sitess array actually has been uploaded, the others have no content and are zero bytes in size.
Any help very much appreciated as we need this project finished soon and I have spent way too much time already. I have been reading up on objects over the weekend and thought I had it worked it out. :-( Thanks very much.
#!/usr/bin/perl -w
use CGI;
my $cgi = new CGI;
my $file = $cgi->param('file');
my $fileName = $cgi->param('file');
@types = ("jpg", "gif");
my ( $type_ok, $file_contents, $buffer);
@sitess=("site1", "site2");
foreach $sitess (@sitess){
print "Content-type: text/html\n\n";
# get the extension
my @file_type = split(/\./, $fileName);
# we can assume everything after the last . found is the extension
my $file_type = $file_type[$#file_type];
# get the file name, this removes everything up to and including the
# last slash found ( be it a forward or back slash )
$fileName =~ s/^.*(\\|\/)//;
# remove all spaces from new instance of filename var
$fileName =~ s/\s+//ig;
# check for any any non alpha numeric characters in filename (allow dots and dahses)
$fileName =~ s/\./PsJsDoT/g;
$fileName =~ s/\-/PsJsDaSh/g;
if($fileName =~ /\W/){
$fileName =~ s/\W/n/ig; # replace any bad chars with the letter "n"
}
$fileName =~ s/PsJsDoT/\./g;
$fileName =~ s/PsJsDaSh/\-/g;
# if $file_type matches one of the types specified, make the $type_ok var true
for($b = 0; $b < @types; $b++){
if($file_type =~ /^$types[$b]$/){
$type_ok++;
}
if($types[$b] eq "ALL"){
$type_ok++; # if ALL keyword is found, increment $type_ok var.
}
}
$overwrite=1;
# if ok, check if overwrite is allowed
if($type_ok){
open ( UPLOADFILE, ">/$sitess/path/$fileName" ) or die "$!";
binmode UPLOADFILE;
#$VAR{err} .= $!;
while (read($file, $buffer, 1024))
{
print UPLOADFILE $buffer;
}
close UPLOADFILE;
print "<font color=\"red\">hi</font></p>";
print "<p>$fileName :: <a href=\" src=\"
}
}
1;
Any help very much appreciated as we need this project finished soon and I have spent way too much time already. I have been reading up on objects over the weekend and thought I had it worked it out. :-( Thanks very much.
#!/usr/bin/perl -w
use CGI;
my $cgi = new CGI;
my $file = $cgi->param('file');
my $fileName = $cgi->param('file');
@types = ("jpg", "gif");
my ( $type_ok, $file_contents, $buffer);
@sitess=("site1", "site2");
foreach $sitess (@sitess){
print "Content-type: text/html\n\n";
# get the extension
my @file_type = split(/\./, $fileName);
# we can assume everything after the last . found is the extension
my $file_type = $file_type[$#file_type];
# get the file name, this removes everything up to and including the
# last slash found ( be it a forward or back slash )
$fileName =~ s/^.*(\\|\/)//;
# remove all spaces from new instance of filename var
$fileName =~ s/\s+//ig;
# check for any any non alpha numeric characters in filename (allow dots and dahses)
$fileName =~ s/\./PsJsDoT/g;
$fileName =~ s/\-/PsJsDaSh/g;
if($fileName =~ /\W/){
$fileName =~ s/\W/n/ig; # replace any bad chars with the letter "n"
}
$fileName =~ s/PsJsDoT/\./g;
$fileName =~ s/PsJsDaSh/\-/g;
# if $file_type matches one of the types specified, make the $type_ok var true
for($b = 0; $b < @types; $b++){
if($file_type =~ /^$types[$b]$/){
$type_ok++;
}
if($types[$b] eq "ALL"){
$type_ok++; # if ALL keyword is found, increment $type_ok var.
}
}
$overwrite=1;
# if ok, check if overwrite is allowed
if($type_ok){
open ( UPLOADFILE, ">/$sitess/path/$fileName" ) or die "$!";
binmode UPLOADFILE;
#$VAR{err} .= $!;
while (read($file, $buffer, 1024))
{
print UPLOADFILE $buffer;
}
close UPLOADFILE;
print "<font color=\"red\">hi</font></p>";
print "<p>$fileName :: <a href=\" src=\"
}
}
1;