CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Uploading Files - PROBLEM!

 



adolfo
Novice

Aug 29, 2001, 1:56 PM

Post #1 of 6 (1162 views)
Uploading Files - PROBLEM! Can't Post

I use these lines to upload files:
open(FILE,">$dir/$filename") || "error $!";
binmode FILE;
while ($bytes = read($pcdir,$buff,2096)) {
$size += $bytes;
print FILE $buff;
}
close(FILE);
But it doesn't upload anything!!! It just creates a zero byte file named $filename
I had chmoded (chmod 777) all the dirs but the problem persists.... =(
Please tell what Im doing wrong !
I would be very happy if somebody tell the correct routine to upload files to my server... ;)
Thanx 4 your help!!

~Adolfo~


mhx
Enthusiast / Moderator

Aug 29, 2001, 9:35 PM

Post #2 of 6 (1159 views)
Re: Uploading Files - PROBLEM! [In reply to] Can't Post

How are you trying to upload? What's the rest of your script? Is this a local or a remote script (I'd say it's a remote script)? What do you want the upload for? Perhaps Net::FTP from the libnet distribution is better suited for up-/downloads, depending on what you're trying to do.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



adolfo
Novice

Aug 30, 2001, 6:43 AM

Post #3 of 6 (1156 views)
Re: Uploading Files - PROBLEM! [In reply to] Can't Post

Hi Marcus,
I'm making a script that allows registered users (remember my question about using the crypt function?) to upload images to our web server using a browser (I work in an ISP). If you have some time I can send you my script file so you can see what's going wrong, the problem is that I wrote it all in spanish =(. When I post a thread I translate all the script vars, etc..)
I'm new at Perl, I'm still learnig, I make all the scripts at my desktop then I upload them to the webserver
Tell me more about Net::FTP from the libnet, the problem is that I know almost nothing about using libs
Here's a basic version of what I did:

#!/usr/bin/perl -w

$| = 1;
use CGI qw(:param);
use CGI::Carp;

$form = new CGI;

$dir = /www/site/images ;
$pcdir=$form->param('pcdir'); # This is what I receive from the form "<input type=file name=pcdir>"

$filename = $pcdir;
$_ = $filename;
s/\w://;
s/([^\/\\]+)$//; # I copied these lines, I don't know nothing about regular expresions
$_ = $1; # well, I read something, almost nothing.... =)
s/\.\.+//g;
s/\s+//g;
$filename = $_;
($name, $ext) = split(/\./, $filename);

open(FILE,">$dir/$filename") || "error $!";
binmode FILE;
while ($bytes = read($pcdir,$buff,2096)) {
$size += $bytes;
print FILE $buff;
}
close(FILE);

Thanx again =)



~Adolfo~


mhx
Enthusiast / Moderator

Aug 30, 2001, 10:38 AM

Post #4 of 6 (1153 views)
Re: Uploading Files - PROBLEM! [In reply to] Can't Post

Now I know what you're trying to do. For this, the CGI modules are sufficient, no Net::FTP required. If you want to know more about Net::FTP, follow the link to CPAN in my last post.

Your code doesn't look too bad to me. I can only spot some missing quotes around the $dir string and the regex part looks a bit strange, but still ok. Perhaps this helps, it's a script I've written a long time ago (I don't even remember the password I need...) which accepts file uploads. Most of the scripts is only checking if this is a valid upload, and the script isn't that nice, but it mainly serves the same purpose as yours. Perhaps you can spot a problem if you compare it with your original code.

Code
#!/usr/bin/perl -w 
use CGI qw(-no_debug :standard);
use CGI::Carp qw(fatalsToBrowser);
use strict;

sub fail ($) { print header, start_html('FAIL:'.shift()), end_html; exit }

if( param() && param('action') && param('passwd') &&
crypt( param('passwd'), '00' ) eq '00PQjU5U6Vi4.' )
{
if( param('action') eq 'upload' )
{
my %files = (
info => { type => 'text/plain', loc => "i" },
hist => { type => 'image/gif', loc => "../p/h" },
lores => { type => 'image/.?jpeg', loc => "../p/lo" },
hires => { type => 'image/.?jpeg', loc => "../p/hi" },
);
my $fail;

foreach( keys %files ) {
$fail = 1;
last unless param($_);
last unless uploadInfo(param($_))->{'Content-Type'} =~ /$files{$_}{type}/;
$fail = 0;
}

unless( $fail )
{
local $/ = undef;
my $root = $ENV{DOCUMENT_ROOT};

$root =~ s/\/(?:www|cgi|cgi-bin)$//;

foreach( keys %files ) {
my $file = param($_);
my $path = "$files{$_}{loc}/"
. ($file =~ /([^:\/\\]+)$/)[0];
open OUT, ">$path" or fail "cannot open '$path': $!";
binmode OUT;
binmode $file;
print OUT <$file>;
close OUT;
}

print header, start_html('SUCCEED'), end_html;
}
}
}

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



adolfo
Novice

Aug 30, 2001, 11:27 AM

Post #5 of 6 (1152 views)
Re: Uploading Files - PROBLEM! [In reply to] Can't Post

Thanx Marcus!! I will check my code.
I followed your link to CPAN, and tried to use the Net::FTP lib that is already installed on my webserver and I wrote this lines to try using this lib but it didn't work again =(

$| = 1;
use CGI qw(:param);
use CGI::Carp;
use Net::FTP;

$form = new CGI;

$dir = /www/site/images ;
$pcdir=$form->param('pcdir'); # This is what I receive from the form "<input type=file name=pcdir>"

$filename = $pcdir;
$_ = $filename;
s/\w://;
s/([^\/\\]+)$//; # I copied these lines, I don't know nothing about regular expresions
$_ = $1; # well, I read something, almost nothing.... =)
s/\.\.+//g;
s/\s+//g;
$filename = $_;
#I copied the lines bellow from http://search.cpan.org/doc/GBARR/libnet-1.0704/Net/libnetFAQ.pod

# for debuging: $ftp = Net::FTP->new('site','Debug',10);
# open a connection and log in!
$ftp = Net::FTP->new('www.uninet.com.py');
$ftp->login('username','mypasswd');
# set transfer mode to binary
$ftp->binary();
# change the directory on the ftp site
$ftp->cwd('$dir/');
# get's arguments are in the following order:
# ftp server's filename
# filename to save the transfer to on the local machine
# can be simply used as get($name) if you want the same name
$ftp->get($filename, $pcdir);
$ftp->site('chmod','0777',$filename);
# ftp done!
$ftp->quit;
If you know how to use this lib take a moment to analize my dirty script and tell what's wrong. For now I will be trying the changes you tell me using CGI modules.
Thanx

~Adolfo~


mhx
Enthusiast / Moderator

Aug 30, 2001, 11:37 AM

Post #6 of 6 (1150 views)
Re: Uploading Files - PROBLEM! [In reply to] Can't Post

Don't mix the Net::FTP code with your script. You don't need Net::FTP. You can't even do anything useful with it concerning your problem.
I'm a bit busy at the moment, so I can't take a deeper look at the script. But using Net::FTP for your problem doesn't make sense to me.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives