How to upload a file from a local pc to a web server from a html page????

S

srini

Hi all,

I am writing a HTML/CGI perl script to attach a file from a HTML
submit form and to store that attachment in a tmeporary variable on
the web server that hosts the perl script.

snippet of code:

HTML part:
---------------
<tr><td colspan="2"> <FONT color=black>Attachment (if any):</FONT><BR>
<INPUT type="file" size=50 name="attachedfile" ALLOW="text/*">
</td></tr>
--------------


Perl code:
-----------------------------------------------
$attachedFile = $query -> param ("attachedfile");
$attachedFile=~ s/.*[\/\\](.*)/$1/;

$upload_filehandle = $query->upload("attachedfile");

$uploaddir = "C:\\Temp"; #### on web server..

$temp = $uploaddir . $attachedFile;

open UPLOADFILE, ">$temp";
close UPLOADFILE;
print "$temp";

------------------------------------------------

Now I want to capture the file attachment from a user's desktop and
upload it into a temp variable on the web server. It is giving me zero
byte sized file in the $temp variable. What am I doing wroing here?

Can some boby kindly tell me how my perl code should be modified so
that I can store the $attachedFile from the user's desktop into $temp
variable on the web server?

One more thing is that I don't want to use filefield() from CGI.pm,
though my script has CGI module. The reason is that all filefield() is
doing is to create another file browse control, which I don' want.

Thanks in advance,
Srini
 
B

Brian McCauley

srini said:
$upload_filehandle = $query->upload("attachedfile");

$uploaddir = "C:\\Temp"; #### on web server..

$temp = $uploaddir . $attachedFile;

open UPLOADFILE, ">$temp";
close UPLOADFILE;
print "$temp";

Now I want to capture the file attachment from a user's desktop and
upload it into a temp variable on the web server. It is giving me zero
byte sized file in the $temp variable.

You getting the CGI upload filehandle then not doing anything with it.

You are opening a file for output withouy checking for success.

You are not writing anything to that file.

You concatenate a directory name and a filename to make a full path name
and forgetting to put a directory separator character in.
What am I doing wroing here?

It would appear you are forgetting the rule:

"You can't just make shit up and expect the computer to know what you mean".
Can some boby kindly tell me how my perl code should be modified so
that I can store the $attachedFile from the user's desktop into $temp
variable on the web server?

If you want to save the stuff to a file you should read stuff from the
filehandle $upload_filehandle and write it to the other one.

But although your code appears to be writing a file you say you actually
want the content of the uploaded in a variable.

You need to get clear in your mind what you are trying to do.

If you want to slurp $upload_filehandle into a variable then do so.
(See FAQ).

You should put directory separators between directory names and filenames.

You should always, yes always, check the success of open.
 
S

srini

OK.. Iam tired of two two things:
1. to look around in this group for a way to upload a file to the web
server
2. to hear the useless and sarcastic answers from some people.

Can some gentlemen pl. tell me how the following code would be
successful?

HTML part:
---------------
<tr><td colspan="2"> <FONT color=black>Attachment (if any):</FONT><BR>
<INPUT type="file" size=50 name="attachedfile" ALLOW="text/*">
</td></tr>
--------------


Perl code:
-----------------------------------------------
$attachedFile = $query -> param ("attachedfile");
$attachedFile=~ s/.*[\/\\](.*)/$1/;

$upload_filehandle = $query->upload("attachedfile");

$uploaddir = "C:\\Temp\\"; #### on web server..

$temp = $uploaddir . $attachedFile;

open UPLOADFILE, ">$temp";
while (<$upload_filehandle>)
{
print UPLOADFILE;
}
close UPLOADFILE;
print "$temp";

------------------------------------------------

The above code gives me a zero sized file on the web server (in
C:\ViewStore folder). How can the above code be modified to get the
correct file from the local PS to the web server???

Thanks in advance..
Srini
 
A

Alan J. Flavell

OK.. Iam tired of two two things:
1. to look around in this group for a way to upload a file to the web
server
2. to hear the useless and sarcastic answers from some people.

Your request for fast-track entry to the killfile has been
enthusiastically approved by the cabal. (TINC).
Can some gentlemen pl. tell me

There's no need to be sexist either.
HTML part:

WTF do you suppose that piece of excrement has got to do with any
Perl-related problem?

The key to successful problem solving is to divide the problem up into
manageable pieces, recognise what is relevant to each, and get each to
work separately before assembling the resulting working components
into a working whole. That's what I'd recommend to you, and - based
on what you chose to post - you're nowhere near there yet.
<INPUT type="file" size=50 name="attachedfile"

Where's your <form> element and its attributes? Haven't you realised
yet that they are far more important than your said:
ALLOW="text/*">

Eh? As the saying goes, "you can't just make shit up and expect it to
work".

Why don't you toddle off to Lincoln Stein's worked examples, which
include file uploading, and take it from there?

bye now
 
S

srini

Alan J. Flavell said:
Your request for fast-track entry to the killfile has been
enthusiastically approved by the cabal. (TINC).


There's no need to be sexist either.


WTF do you suppose that piece of excrement has got to do with any
Perl-related problem?

The key to successful problem solving is to divide the problem up into
manageable pieces, recognise what is relevant to each, and get each to
work separately before assembling the resulting working components
into a working whole. That's what I'd recommend to you, and - based
on what you chose to post - you're nowhere near there yet.


Where's your <form> element and its attributes? Haven't you realised


Eh? As the saying goes, "you can't just make shit up and expect it to
work".

Why don't you toddle off to Lincoln Stein's worked examples, which
include file uploading, and take it from there?

bye now

Thanks Allen for your time..

I just gave a snippet of HTML code from where the type ="file" to make
sure that I am taking the file upload variable from the html form and
to differentiate from the CGI funciton filefield(), which throws
another browse component and I don' the latter.

Coming ot the point, can you find some bug and tell me how to rectify
it in the perl script or not? Otherwise, why do you waste your energy?

Thanks,
Srini
 
J

John Bokma

(e-mail address removed) (srini) wrote in
Coming ot the point, can you find some bug and tell me how to rectify
it in the perl script or not? Otherwise, why do you waste your energy?

To stop you and others from wasting more time of others :-D. Usenet is not
a free helpdesk.
 
J

Joe Smith

srini said:
open UPLOADFILE, ">$temp";
while (<$upload_filehandle>)
{
print UPLOADFILE;
}
close UPLOADFILE;

1) You should *always* check the results of an open() call.
2) Magic with $_ happens for <>, not for any other read operation,
which means that you have to explictly set $_.

use CGI::Carp qw:)fatalsToBrowser);
open UPLOADFILE, '>', $temp or die "Cannot create file $temp: $!\n";
binmode $upload_filehandle;
binmode UPLOADFILE;
while(defined($_ = <$upload_filehandle>)) {
print UPLOADFILE or warn "Possible disk full error: $!\n";
}
close UPLOADFILE or warn "Possible truncation on $temp: $!\n";

-Joe
 
G

gnari

Joe Smith said:
1) You should *always* check the results of an open() call.
2) Magic with $_ happens for <>, not for any other read operation,
which means that you have to explictly set $_.

what magic are you talking about, exactly ?

gnari
 
P

Paul Lalli

1) You should *always* check the results of an open() call.
2) Magic with $_ happens for <>, not for any other read operation,
which means that you have to explictly set $_.

Who on earth told you that? The auto assigning of $_ to the results of <>
happens regardless of whether or not a filehandle is given to <>.

open my $file, 'file.txt' or die "Cannot open file: $!";
while (<$file>){
print;
}

__END__

Try it yourself. Run the above code. The 'magic' you refer to only fails
to occur if there is anything in the while conditional other than the <>
operator. This does not include reading from a given filehandle instead
of implicitly from $ARGV.

while (<>) { ... } #works
while (<STDIN>) { ... } #works
while (<FILE>) { ... } #works
while (<> and $debug) { ... } #does not work

If your mistaken belief came from some site or book, please let us know
what it is so we know to avoid it.


Paul Lalli
 
S

srini

Joe Smith said:
1) You should *always* check the results of an open() call.
2) Magic with $_ happens for <>, not for any other read operation,
which means that you have to explictly set $_.

use CGI::Carp qw:)fatalsToBrowser);
open UPLOADFILE, '>', $temp or die "Cannot create file $temp: $!\n";
binmode $upload_filehandle;
binmode UPLOADFILE;
while(defined($_ = <$upload_filehandle>)) {
print UPLOADFILE or warn "Possible disk full error: $!\n";
}
close UPLOADFILE or warn "Possible truncation on $temp: $!\n";

-Joe


Thanks Joe,

I tried as follows:
-------------------------
$attachedFile = $query -> param ("attachedfile");

$attachedFile=~ s/.*[\/\\](.*)/$1/;

$upload_filehandle = $query->upload("attachedfile");

$uploaddir = "C:\\ViewStore\\"; #### on web server..

$temp = $uploaddir . $attachedFile;

open UPLOADFILE, '>', $temp or die "Cannot create file $temp: $!\n";
binmode $upload_filehandle;
binmode UPLOADFILE;

while(defined($_ = <$upload_filehandle>))
{
print UPLOADFILE or warn "Possible disk full error: $!\n";
}

close UPLOADFILE;

print "$temp";
------------------------

But I still see empty file on the web server....
Any ideas why I ma getting only empty file there?

Thanks,
Srini
 
C

ctcgag

Thanks Joe,

I tried as follows:
-------------------------
$attachedFile = $query -> param ("attachedfile");

$attachedFile=~ s/.*[\/\\](.*)/$1/;

$upload_filehandle = $query->upload("attachedfile");

$uploaddir = "C:\\ViewStore\\"; #### on web server..

$temp = $uploaddir . $attachedFile;

open UPLOADFILE, '>', $temp or die "Cannot create file $temp: $!\n";
binmode $upload_filehandle;
binmode UPLOADFILE;

while(defined($_ = <$upload_filehandle>))
{
print UPLOADFILE or warn "Possible disk full error: $!\n";
}

close UPLOADFILE;

print "$temp";


Is it dying or giving you any warnings? If it were, would you know?

Xho
 
J

Jonathan Tree

<SNIPPED STUFF>

You still have not shown all parts of the problem domain. What does
your <FORM> statement look like in the HTML? Have you perused Lincoln
Stein's examples? Does your <FORM> statement look like this:

<FORM METHOD="POST" ENCTYPE="multipart/form-data"
ACTION="<your own upload process>">


You cannot expect quality replies if you withhold vital information.
 
B

Brian McCauley

srini said:
open UPLOADFILE, '>', $temp or die "Cannot create file $temp: $!\n";
binmode $upload_filehandle;
binmode UPLOADFILE;

while(defined($_ = <$upload_filehandle>))
{
print UPLOADFILE or warn "Possible disk full error: $!\n";
}

close UPLOADFILE;
But I still see empty file on the web server....
Any ideas why I ma getting only empty file there?

If the disk really were full you'd possibly see an error on close than
on print if you are testing this with a small file. (Perl uses buffered
I/O).

But I'm inclined to agree with other contributors to this thread that
the real problem lies in stuff that you've not shown us.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
A

Alan J. Flavell

A feel the need to correct myself on one point:

Where's your <form> element and its attributes? Haven't you realised
yet that they are far more important than your <font> and <br> crap?

(I stand by that part...)
Eh? As the saying goes, "you can't just make shit up and expect it to
work".

The unfortunate juxtaposition of that last remark and the quoted
fragment which precedes it was a mistake on my part. Sorry about
that.

(I'd still want to see a test-case, without extraneous
clutter, but complete enough to be actually runnable.)
 
B

Brian McCauley

Alan said:
A feel the need to correct myself on one point:

On the contrary, there's no need to correct yourself. On Usenet there
are thousands of people just waiting to leap at the chance to correct
you. :)
The unfortunate juxtaposition of that last remark and the quoted
fragment which precedes it was a mistake on my part.

Actually while the juxtaposition may have been unitended it was, I
think, not inappropriate.

The correct attribute name is 'accept' not 'allow'.

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
A

Alan J. Flavell

Actually while the juxtaposition may have been unitended it was, I
think, not inappropriate.

;-) thanks. Two heads are evidently better than one (cue HHGTTG)
 
B

Brian McCauley

Brian said:
If the disk really were full you'd possibly see an error on close than
on print if you are testing this with a small file. (Perl uses buffered
I/O).

Because I'm a kind and generous person even though it has nothing to do
with Perl, I will point out that it could be a permisions issue. I know
the OP is using Windows and it is posible on Windows (NTFS) to set
persisions on a directory so that a user can create empty files but not
put anything in them.
 
T

Tad McClellan

Brian McCauley said:
On the contrary, there's no need to correct yourself. On Usenet there
are thousands of people just waiting to leap at the chance to correct
you. :)


I think that you are wrong there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top