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

Discussion in 'Perl Misc' started by srini, Aug 19, 2004.

  1. srini

    srini Guest

    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
     
    srini, Aug 19, 2004
    #1
    1. Advertising

  2. Re: How to upload a file from a local pc to a web server from ahtml page????

    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 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.
     
    Brian McCauley, Aug 19, 2004
    #2
    1. Advertising

  3. srini

    srini Guest

    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 <> wrote in message news:<cg1lg7$tki$>...
    > 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 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.
     
    srini, Aug 19, 2004
    #3
  4. Re: How to upload a file from a local pc to a web server from ahtml page????

    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 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
     
    Alan J. Flavell, Aug 19, 2004
    #4
  5. srini <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Aug 20, 2004
    #5
  6. srini

    srini Guest

    "Alan J. Flavell" <> wrote in message news:<>...
    > 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 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
     
    srini, Aug 20, 2004
    #6
  7. srini

    John Bokma Guest

    John Bokma, Aug 20, 2004
    #7
  8. srini

    Joe Smith Guest

    Re: How to upload a file from a local pc to a web server from ahtml page????

    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
     
    Joe Smith, Aug 20, 2004
    #8
  9. srini

    gnari Guest

    Re: How to upload a file from a local pc to a web server from ahtml page????

    "Joe Smith" <> 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
     
    gnari, Aug 20, 2004
    #9
  10. srini

    Paul Lalli Guest

    Re: How to upload a file from a local pc to a web server from ahtml page????

    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
     
    Paul Lalli, Aug 20, 2004
    #10
  11. srini

    srini Guest

    Joe Smith <> 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 $_.
    >
    > 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
     
    srini, Aug 20, 2004
    #11
  12. srini

    Guest

    (srini) wrote:

    > 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?



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

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Aug 20, 2004
    #12
  13. (srini) wrote in message news:<>...
    > Joe Smith <> wrote in message news:<rPgVc.289630$%_6.59237@attbi_s01>...
    > > srini wrote:
    > >


    <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.
     
    Jonathan Tree, Aug 20, 2004
    #13
  14. Re: How to upload a file from a local pc to a web server from ahtml page????

    srini wrote:
    > 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\\
     
    Brian McCauley, Aug 20, 2004
    #14
  15. Re: How to upload a file from a local pc to a web server from ahtml page????

    A feel the need to correct myself on one point:

    On Thu, 19 Aug 2004, Alan J. Flavell wrote:

    > > <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?


    (I stand by that part...)

    > > ALLOW="text/*">

    >
    > 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.)
     
    Alan J. Flavell, Aug 20, 2004
    #15
  16. Re: How to upload a file from a local pc to a web server from ahtml page????

    Alan J. Flavell wrote:

    > 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. :)

    > On Thu, 19 Aug 2004, Alan J. Flavell wrote:
    >>>ALLOW="text/*">

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


    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\\
     
    Brian McCauley, Aug 20, 2004
    #16
  17. Re: How to upload a file from a local pc to a web server from ahtml page????

    On Fri, 20 Aug 2004, Brian McCauley wrote:

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


    ;-) thanks. Two heads are evidently better than one (cue HHGTTG)
     
    Alan J. Flavell, Aug 20, 2004
    #17
  18. Re: How to upload a file from a local pc to a web server from ahtml page????

    Brian McCauley wrote:
    > srini wrote:
    >
    >> 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).


    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.
     
    Brian McCauley, Aug 20, 2004
    #18
  19. Re: How to upload a file from a local pc to a web server from a html page????

    Brian McCauley <> wrote:
    > Alan J. Flavell wrote:
    >
    >> 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. :)



    I think that you are wrong there.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Aug 20, 2004
    #19
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FyYXY=?=

    Upload a file without file Upload control - ASP.Net

    =?Utf-8?B?U2FyYXY=?=, Aug 2, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,505
    Bruce Barker
    Aug 3, 2005
  2. DigitalUncle
    Replies:
    0
    Views:
    940
    DigitalUncle
    Nov 2, 2004
  3. Lyndon
    Replies:
    1
    Views:
    532
    Leonard Blaisdell
    Jul 25, 2005
  4. Gregory A. Beamer

    Re: MVC upload local file to server

    Gregory A. Beamer, Dec 20, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    660
    Gregory A. Beamer
    Dec 20, 2008
  5. Prakash
    Replies:
    3
    Views:
    480
    Ray at
    Nov 12, 2003
Loading...

Share This Page