Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to upload a file from a local pc to a web server from a html page????

Reply
Thread Tools

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

 
 
srini
Guest
Posts: n/a
 
      08-19-2004
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
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      08-19-2004
srini wrote:

> $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 **** 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.

 
Reply With Quote
 
 
 
 
srini
Guest
Posts: n/a
 
      08-19-2004
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


Brian McCauley <(E-Mail Removed)> wrote in message news:<cg1lg7$tki$(E-Mail Removed)>...
> srini wrote:
>
> > $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 **** 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.

 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      08-19-2004
On Thu, 19 Aug 2004, srini blurted out atop a fullquote:

> 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:
> ---------------
> <tr><td colspan="2"> <FONT color=black>Attachment (if any):</FONT><BR>


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 <font> and <br> crap?

> ALLOW="text/*">


Eh? As the saying goes, "you can't just make **** 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
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      08-19-2004
srini <(E-Mail Removed)> wrote:

> OK.. Iam tired of two two things:



> 2. to hear the useless and sarcastic answers from some people.



So long.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
srini
Guest
Posts: n/a
 
      08-20-2004
"Alan J. Flavell" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed). gla.ac.uk>...
> On Thu, 19 Aug 2004, srini blurted out atop a fullquote:
>
> > 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:
> > ---------------
> > <tr><td colspan="2"> <FONT color=black>Attachment (if any):</FONT><BR>

>
> 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 <font> and <br> crap?
>
> > ALLOW="text/*">

>
> Eh? As the saying goes, "you can't just make **** 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
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      08-20-2004
(E-Mail Removed) (srini) wrote in
news:(E-Mail Removed) om:

> 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 . Usenet is not
a free helpdesk.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      08-20-2004
srini wrote:

> 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
 
Reply With Quote
 
gnari
Guest
Posts: n/a
 
      08-20-2004
"Joe Smith" <(E-Mail Removed)> wrote in message
news:rPgVc.289630$%_6.59237@attbi_s01...
> srini wrote:
>
> > 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 $_.


what magic are you talking about, exactly ?

gnari




 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-20-2004
On Fri, 20 Aug 2004, Joe Smith wrote:

> srini wrote:
>
> > 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 $_.


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: MVC upload local file to server Gregory A. Beamer ASP .Net 0 12-20-2008 08:47 PM
Upload a file without file Upload control - ASP.Net =?Utf-8?B?U2FyYXY=?= ASP .Net 3 08-03-2005 01:09 AM
Playing a local mpeg file from a local HTML file... Lyndon HTML 1 07-25-2005 02:21 AM
Upload Excel with Macros using HTML File Upload Prakash ASP General 3 11-12-2003 04:26 AM



Advertisments