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

object oriented programming : Upload file to multiple sites

Status
Not open for further replies.

fortwilliam

Programmer
Mar 5, 2008
7
0
0
GB
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;
 
Can't see where you open [tt]$file[/tt] to [tt]read[/tt] it: seems there's some kind of magic opening. Anyway, as you don't roll back the pointer to [tt]$file[/tt], subsequent [tt]read[/tt]s after the first site return nothing, as the file is at [tt]eof[/tt]

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Hi,

Thanks for the reply.

I thought this read the file content and the filename:-
my $file = $cgi->param('file');
my $fileName = $cgi->param('file');

The file is sent from a regular html form:-
input type="file" size="30" name="file">

I have tried :-
my $file = $cgi->param('file');
my $fileName = $cgi->param('file');

: inside the foreach loop but it still does not work.
 
Not sure what you want to do with [tt]my $file = $cgi->param('file');[/tt] : the parameter [tt]file[/tt] contains a filename, then to [tt]read[/tt] it you need to [tt]open[/tt] it...

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
The perl script will only be able to upload a file to the server it runs on. If you want to upload a file to more than one server you will need to use something like NET::Ftp to transfer the file to the server the script is not running on. Otherwise could you imagine the problems if a perl script running on server 'A' could upload a file to server 'B'? Bad, very bad.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'm assuming the sites are all on the same server.

Code:
    open ( UPLOADFILE, ">/$sitess/path/$fileName" ) or die "$!";

So it would write to the local path /site1/path/$fileName and /site2/path/$fileName. Why you'd have the home directory for the websites be / is beyond me though and sounds messy.

A couple things:

1. Why do you expect these two variables to get different values?

Code:
$file = $cgi->param('file');
$fileName = $cgi->param('file');

I think what you wanted was this:

Code:
$file = $cgi->upload('file');
$fileName = $cgi->param('file');

CGI::upload returns a filehandle for the file's data. CGI::param returns the name of the file from the field on the form. You were using param for both, so both $file and $fileName would equal the name of the file, and neither one would be the filehandle.

2. Your script determines $file and $fileName at the top of the script, before the foreach loop. Are you uploading the exact same file to multiple sites? If so, you might optimize it by having your script do all the file reading and validation and everything before the foreach loop, and then in the foreach all you need to do is simply write the file to disk.

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top