Form Post Issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philip D Heady

    Form Post Issues

    I have a webpage that uploads a photo picture file to server, but goes
    through some error checking before it redirects to the next page. Since I do
    not have PHP compiled with mime type support I have checking last 3
    characters of filename. Here are my problems:

    1) When I set form type to ENCTYPE="multip art/form-data" i can check the
    file size correctly and works fine, but then filename and extension are in
    some weird format which is binary I believe.

    form = 1
    Photo: /tmp/phpnDF01W
    image_x = 89
    image_y = 16
    Ext: 01w < this is wrong, should be gif/jpg/etc...
    Size: 34326
    File: /tmp/phpnDF01W < this is wrong

    2) When I omit the above form type and use default, file size function fails
    but the extension and filename work correctly. I believe this is because it
    is sent in ascii/plain text mode.

    Warning: filesize(): Stat failed for C:\\WINDOWS\\De sktop\\sci.psd
    (errno=2 - No such file or directory) in
    /home/fcl/public_html/free/index3.php on line 30

    form = 1
    photo = C:\\WINDOWS\\De sktop\\sci.psd
    image_x = 66
    image_y = 12
    Ext: psd
    Size: ??????
    File = C:\\WINDOWS\\De sktop\\sci.psd

    3) I'm also trying to get the file to the server and it's not uploading it.
    It's creating the directories but not moving the file up there for me!!


    4) Here is code. What is going on? Can I kill 3 birds with one stone here???
    I'd rather not recompile PHP. Must I convert $ext from binary to plain
    text??

    while(list($key , $value) = each($HTTP_GET_ VARS)) {

    echo "$key = $value<br>";
    }


    if ($form) {

    if ($photo) {

    $ext = strtolower(subs tr($photo, -3));

    function fsize($photo) {
    return filesize($photo );
    }

    $size = fsize($photo);

    if ($size > 100000) {

    $focus = "photo";
    $msg = "Your file size exceeds the maximum allowed
    upload. Please re-submit or skip this step.";

    } elseif (!preg_match("/(gif|peg|jpg|bm p|png)$/",
    $ext)) {

    $focus = "photo";
    $msg = "Invalid file type. Please submit a gif, jpeg,
    bmp, or png photo.";

    } else {

    if ($ltype == "1") { $type = "hotel"; }<-- this all works fine, etc
    if ($ltype == "2") { $type = "inn"; }
    if ($ltype == "3") { $type = "bandb"; }
    if ($ltype == "4") { $type = "resort"; }

    $imgdir = "$DOCUMENT_ ROOT/lodging/$type"; <-- this all works fine,etc
    if (!file_exists($ imgdir)) { mkdir($imgdir, 0755); }
    $imgdir.= "/$aid";
    if (!file_exists($ imgdir)) { mkdir($imgdir, 0755); }

    move_uploaded_f ile($photo, "$imgdir/photo1.jpg"); <---this is not
    working!!

    loc("index4.php ?sid=$sid&aid=$ aid"); <-- works fine

    }}


    <form name="form1" method="post"
    action="<?=$SCR IPT_NAME?>?sid= <?=$sid?>&aid=< ?=$aid?>&ltype= <?=$ltype?>"
    ENCTYPE="multip art/form data">
    <input type="hidden" name="form" value="1">





  • gmuldoon

    #2
    Re: Form Post Issues

    pdheady@comcast .net says...[color=blue]
    > I have a webpage that uploads a photo picture file to server, but goes
    > through some error checking before it redirects to the next page. Since I do
    > not have PHP compiled with mime type support I have checking last 3
    > characters of filename. Here are my problems:
    >
    > 1) When I set form type to ENCTYPE="multip art/form-data" i can check the
    > file size correctly and works fine, but then filename and extension are in
    > some weird format which is binary I believe.
    >
    > form = 1
    > Photo: /tmp/phpnDF01W
    > image_x = 89
    > image_y = 16
    > Ext: 01w < this is wrong, should be gif/jpg/etc...
    > Size: 34326
    > File: /tmp/phpnDF01W < this is wrong[/color]

    No, it's not wrong. It's just the unique temporary name PHP gives to
    the temporary copy of the file. The file name attribute is
    independently available to you, just not there. Try this:

    <?php
    if (!isset($infile )) {
    echo '
    <form action="'.$PHP_ SELF.'" ENCTYPE="multip art/form-data"
    method=post>
    <input type=file name=infile><br >
    <input type=submit>
    </form>
    ';
    } else {
    echo '
    File Name:'.$infile_ name.' is '.$infile_size. ' bytes in size.
    ';
    }
    ?>

    Note the name of the file ($infile) in the form and the use of the
    system-generated variables $infile_name and $infile_size.

    Not that I'd ever use that approach. For starters, I'd want to rename
    the file to a unique value, something like a database record number.
    [color=blue]
    > } elseif (!preg_match("/(gif|peg|jpg|bm p|png)$/",$ext)) {[/color]

    And I'd never trust end-user input to be that correct. Mac users among
    others aren't likely to bother with file name extensions at all.

    Use the in-built image functions to get the definitive image type.

    $imagestats=get ImageSize($infi le);

    if ($imagestats[2]=1) {
    copy ($infile, "images/user1.gif");
    } elseif ($imagestats[2]=2) {
    copy ($infile, "images/user1.jpg");
    } elseif ($imagestats[2]=3) {
    copy ($infile, "images/user1.png");
    } elseif ($imagestats[2]=6) {
    copy ($infile, "images/user1.bmp");
    } elseif ($imagestats[2]=7) {
    copy ($infile, "images/user1.tiff");
    } elseif ($imagestats[2]=9) {
    copy ($infile, "images/user1.tiff");
    } else {
    echo '
    Sorry user1, supplied file is not a supported image type!
    ';
    }

    Cheers,

    Geoff M


    Comment

    • Philip D Heady

      #3
      Re: Form Post Issues

      Thank you. This works like a charm!

      $imagestats=get ImageSize($phot o);

      $imgdir = "$DOCUMENT_ ROOT/lodging/$type";
      if (!file_exists($ imgdir)) { mkdir($imgdir, 0755); }
      $imgdir.= "/$aid";
      if (!file_exists($ imgdir)) { mkdir($imgdir, 0755); }

      if ($photo_size > 100000) {

      $focus = "photo";
      $msg = "Your file size exceeds the maximum allowed upload of
      100k.<br>Please re-submit or skip this step.";

      } elseif (!preg_match("/(1|2|3|6|7|8)$/", $imagestats[2])) {

      $focus = "photo";
      $msg = "Invalid file type. Please submit a gif, jpeg, bmp, png, or tiff
      photo. Or skip this step.";

      } else { etc..



      Comment

      Working...