By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,709 Members | 2,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,709 IT Pros & Developers. It's quick & easy.

object oriented programming : Upload file to multiple sites

P: 2
Hi, I am very new to "object oriented programming". I have this script which I didn't write but have altered and 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. Thanks very much.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. use CGI;
  4.  
  5. my $cgi = new CGI;
  6.  
  7. my $file = $cgi->param('file');
  8. my $fileName = $cgi->param('file');
  9.  
  10.  
  11.  
  12. @types = ("jpg", "gif");
  13. my ( $type_ok, $file_contents, $buffer);
  14.  
  15. @sitess=("site1", "site2");
  16.  
  17. foreach $sitess (@sitess){
  18.  
  19. print "Content-type: text/html\n\n";
  20.  
  21.     # get the extension
  22.     my @file_type   = split(/\./, $fileName);
  23.     # we can assume everything after the last . found is the extension
  24.     my $file_type   = $file_type[$#file_type];
  25.  
  26.     # get the file name, this removes everything up to and including the 
  27.     # last slash found ( be it a forward or back slash )
  28.     $fileName =~ s/^.*(\\|\/)//;
  29.  
  30.     # remove all spaces from new instance of filename var 
  31.     $fileName =~ s/\s+//ig;
  32.  
  33.     # check for any any non alpha numeric characters in filename (allow dots and dahses)
  34.     $fileName =~ s/\./PsJsDoT/g;
  35.     $fileName =~ s/\-/PsJsDaSh/g;
  36.     if($fileName =~ /\W/){
  37.         $fileName =~ s/\W/n/ig; # replace any bad chars with the letter "n"
  38.     }
  39.     $fileName =~ s/PsJsDoT/\./g;
  40.     $fileName =~ s/PsJsDaSh/\-/g;
  41.  
  42.     # if $file_type matches one of the types specified, make the $type_ok var true
  43.     for($b = 0; $b < @types; $b++){
  44.  
  45.         if($file_type =~ /^$types[$b]$/){
  46.             $type_ok++;
  47.         }
  48.         if($types[$b] eq "ALL"){
  49.             $type_ok++; # if ALL keyword is found, increment $type_ok var.
  50.         }
  51.     }
  52.     $overwrite=1;
  53.     # if ok, check if overwrite is allowed
  54.     if($type_ok){
  55.  
  56.     open ( UPLOADFILE, ">/$sitess/path/$fileName" ) or die "$!"; 
  57. binmode UPLOADFILE; 
  58. #$VAR{err} .= $!;
  59. while  (read($file, $buffer, 1024)) 
  60.  print UPLOADFILE $buffer; 
  61.  
  62. close UPLOADFILE; 
  63.  
  64. print "<font color=\"red\">hi</font></p>";
  65.  
  66. print "<p>$fileName :: <a href=\"http://www.$sitess.com/$fileName\"><img src=\"http://www.$sitess.com/$fileName\"></a></p>";
  67.  
  68. }
  69.  
  70. }
  71.  
  72. 1;
  73.  
Sep 2 '08 #1
Share this Question
Share on Google+
1 Reply


KevinADC
Expert 2.5K+
P: 4,059
see tek-tips
Sep 3 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.