Results 1 to 12 of 12
  1. #1
    Lounger
    Join Date
    Jun 2001
    Location
    South Australia, Australia
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Attach Files to Forms

    Hello All,

    Can anyone tell me of the correct method, or an appropriate cgi, which will enable me to set a html form up so a user can attach a file?

    The <input type=file etc etc> is supposed to work, but it doesn't, so I'm assuming I'm down to cgis. I've looked at the source of this page, and seen that there is a <input type=file> form field, but also that the action goes to a cgi. Does this mean that the type=file needs to be connected to a cgi, or not?

    In fact, a form exactly like the one I'm typing into, complete with cgi, would be just right! <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    Thanks!

  2. #2
    Lounger
    Join Date
    Mar 2002
    Location
    Tampa, Florida, USA
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attach Files to Forms

    Well, since no one else has taken a shot at this yet, I'll give it a try. But I am not comfortable with my expertise here.

    I do now know how you could handle it without CGI. The file the user uploads has to go somewhere (on the server, I would assume), and I would think you need CGI to get it there.

    (I suppose it might be possible to make it an attachment to an e-mail if the action were mailto, but I have no idea if or how that would work.)

    I am doing it successfully by calling a PHP program, in part, as follows:

    <form name=uppic method="post" enctype="multipart/form-data" action="upload.php3">

    <input type="file" name="file1" size=30 maxlength=60>

    PHP grabs the file1 variable as an object. One of the properties is the file name, which PHP calls $file1_name. In my PHP CGI, I copy this file name to a directory on my server. Works for me, though I do not claim to understand it completely.

    You can see the complete form I use at the bottom of my home page: http://www.bbhq.com - if that is of any help.

    - hmc

  3. #3
    Lounger
    Join Date
    Jun 2001
    Location
    South Australia, Australia
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attach Files to Forms

    Thanks for this. It looks exactly what I'm after. I had a look at the form on your site, also. What would really help is if I could see the php3 file "upload.php3". If this isn't possible, then that's fine, but it would probably make sense of the whole thing for me. Perhaps you can let me know on this one...

    Thanks.

  4. #4
    Lounger
    Join Date
    Mar 2002
    Location
    Tampa, Florida, USA
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attach Files to Forms

    I think I sent you the code via a private message on this forum. Let me know if you did not get it.

    - hmc

    hmc@bbhq.com

  5. #5
    Plutonium Lounger Leif's Avatar
    Join Date
    Dec 2000
    Location
    U.K.
    Posts
    14,010
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attach Files to Forms

    Can you post it here to share with all, please?

  6. #6
    Lounger
    Join Date
    Mar 2002
    Location
    Tampa, Florida, USA
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attach Files to Forms

    Well, yes I can. And since you asked, and since this board has been so helpful to me, I will. But I do not want to be remembered for this script. I am much better than this indicates.... really. Still... it does work.

    This is an edited version of the PHP script that accepts the form datat containing a file name and stores in on my server:

    <script language="PHP">
    $mmddyy = date("m/d/y");
    $hhmm = date("hi") ;
    $isok = "no" ;
    echo "<center><h2><font color="#00009C">" ;
    echo "BBHQ.COM File Upload" ;
    echo "</font></h3></center>" ;
    //
    if ($file1 == "none") {
    echo "Sorry, your computer could not find $file1_name on your computer. ";
    echo "Please press the BACK button on your browser to try again.

    n" ;
    }
    if (!preg_match("/.bmp/i", "$file1_name") && !preg_match("/.jpg/i", "$file1_name") && !preg_match("/.gif/i", "$file1_name") ) {
    echo "Sorry, $file1_name is not a valid file type. Your file must be a .BMP, .JPG, or .GIF file.

    n" ;
    echo "Please press the BACK button on your browser to try again.

    n" ;
    exit ;
    }
    set_time_limit(60); // make reasonably sure the script does not time out on large files
    $path1 = dirname($PATH_TRANSLATED)."/xxxxxxx/";
    $source = $file1; // file1 now has a temporary name - it is an object
    $source_name = $file1_name;
    if (($source <> "none") && ($source <> "")) { // see if the file exists; non-existing file has name of "none"
    $dest = $path1.$source_name; // append file name to our path
    if (copy($source,$dest)) { // copy the file from the temp directory to the upload directory, and test for success
    // this is a bmp file:
    if (preg_match("/.bmp/i", "$file1_name") ) { // bmp file
    // time() added to file name assures unique name
    $ending .= time() . ".bmp";
    $newname = $path1;
    $newname .= $ending ;
    if (copy($dest,$newname) ) {
    $isok = "yes" ;
    }
    else {
    echo "Sorry - something seems to have gone wrong at our end. Please press ";
    echo "the BACK button on your browser to try again.

    n" ;
    }
    }
    // this is a jpg file:
    if (preg_match("/.jpg/i", "$file1_name") ) {
    $ending .= time() . ".jpg";
    $newname = $path1;
    $newname .= $ending ;
    if (copy($dest,$newname) ) {
    $isok = "yes" ;
    }
    else {
    echo "Sorry - something seems to have gone wrong at our end. Please press ";
    echo "the BACK button on your browser to try again.

    n" ;
    }
    }
    // this is a gif file:
    if (preg_match("/.gif/i", "$file1_name") ) {
    $ending .= time() . ".gif";
    $newname = $path1;
    $newname .= $ending ;
    if (copy($dest,$newname) ) {
    $isok = "yes" ;
    }
    else {
    echo "Sorry - something seems to have gone wrong at our end. Please press ";
    echo "the BACK button on your browser to try again.

    n" ;
    }
    }
    } else { // copy did not work
    echo "Sorry - something seems to have gone wrong at our end. Please press ";
    echo "the BACK button on your browser to try again.

    n" ;
    }
    unlink($source); // delete the file from the temp directory
    } else { // should never happen
    echo "Sorry - something seems to have gone wrong at our end. Please press ";
    echo "the BACK button on your browser to try again.

    n" ;
    echo "</body></html>";
    exit ;
    }
    if ($isok == "yes" ) {
    echo "Thanks; we have received your picture. It looks great! (I think).

    n" ;
    echo "<center>[img]./xxxxxxx/$ending[/img]</center>

    n" ;
    } else {
    echo "Sorry - something seems to have gone wrong at our end. Please press ";
    echo "the BACK button on your browser to try again.

    n" ;
    }
    </script>

  7. #7
    Lounger
    Join Date
    Jun 2001
    Location
    South Australia, Australia
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attach Files to Forms

    Well, I had a go at implementing this with php, but

    1)as usual, my isp hasn't been that helpful
    2)I was hoping for something that might not be php dependent

    From looking at the number of views of this thread, it seems to be something a lot of people are interested in...

    Is there no-one out there who knows a simple cgi script to achieve this?

    Thanks in anticipation...

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Attach Files to Forms

    A little web search revealed the following:

    <A target="_blank" HREF=http://cgi-lib.berkeley.edu/>The cgi-lib.pl Home Page</A>

    This page features a couple of illustrations of how to use the cgi-lib.pl library to do file uploading. I think your ISP probably supports perl, maybe even this very library.

  9. #9
    Silver Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles, California, USA
    Posts
    1,734
    Thanks
    0
    Thanked 3 Times in 1 Post

    Re: Attach Files to Forms

    <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15> We use a 'simple little cgi script' all the time here. Haven't you noticed it on the form you use to post your messages?
    <IMG SRC=http://www.wopr.com/w3tuserpics/Eileen_sig.gif>

  10. #10
    Lounger
    Join Date
    Jun 2001
    Location
    South Australia, Australia
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attach Files to Forms

    Hi Eileen,

    Yes! I noticed you must use such a script the first time I was searching for them. In fact, my first post on this thread contained the following:

    "In fact, a form exactly like the one I'm typing into, complete with cgi, would be just right! "

    What I was hoping was that someone here would be good enough to provide me with the cgi used, since I obviously can't access it. But either no-one understood my little hint, or no-one wanted to give it away!

    I assumed it was the latter...

    If I'm mistaken, can somebody here possibly make available the cgi used to me? (Just from looking at the source, I see that the file I need is addpost.pl)

    In hope, and with thanks...

  11. #11
    Silver Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles, California, USA
    Posts
    1,734
    Thanks
    0
    Thanked 3 Times in 1 Post

    Re: Attach Files to Forms

    Sorry for teasing. I can't give you the full script because wwwthreads is a commercial package but, when I have a minute <img src=/S/laugh.gif border=0 alt=laugh width=15 height=15>, I'll pull out the relevant bit of code for you.
    <IMG SRC=http://www.wopr.com/w3tuserpics/Eileen_sig.gif>

  12. #12
    Lounger
    Join Date
    Jun 2001
    Location
    South Australia, Australia
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attach Files to Forms

    Aaaah. That would be great Eileen. Thanks! And I wait in anticipation!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •