SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    Greenville, SC
    Posts
    388
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    file upload left blank.

    Hello folks,

    I have yet another issue. (who's surprised)

    I'm using the following code
    PHP Code:
    if ($_FILES['img_exterior']['type'] == "image/gif" or $_FILES['img_exterior']['type'] == "image/pjpeg"){ 
    move_uploaded_file($_FILES['img_exterior']['tmp_name'], "images/".$_FILES['img_exterior']['name']) 
        or die (
    "Could not copy"); }
    else { 
     
                echo 
    "Could Not Copy, Wrong Filetype (".$_FILES['img_exterior']['name'].")"
     
            } 
    This code is on an update page. Basically the image name is stored into the database if uploaded. But in the update page, most times the image is already uploaded and the file name stored in the database. If I call up the update page to make some modifications other than the file upload (meaning the image upload form field is left blank) I get the following error:

    Could Not Copy, Wrong Filetype ()

    my question is how do I modify the code to handle the updates even with file upload fields left blank. (meaning the user doesn't wish to modify the image name or upload another file just modify other parts of the form) ??

  2. #2
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sure there must be an easier solution but this might work as well.

    PHP Code:
    if ($_FILES['img_exterior']['tmp_name'] != "") {

       if (
    $_FILES['img_exterior']['type'] == "image/gif" or $_FILES['img_exterior']['type'] == "image/pjpeg"){ 
    move_uploaded_file($_FILES['img_exterior']['tmp_name'], "images/".$_FILES['img_exterior']['name']) 
        or die (
    "Could not copy"); }
       else { 

                echo 
    "Could Not Copy, Wrong Filetype (".$_FILES['img_exterior']['name'].")"

            } 

    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine

  3. #3
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    Greenville, SC
    Posts
    388
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Toly, that helps. However, Now the original file name is being deleted from the database where I want it left alone if the form field is left blank.... Anybody have any Ideas how to fix this??

  4. #4
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jive
    However, Now the original file name is being deleted from the database where I want it left alone if the form field is left blank
    I'm guessing this is because your INSERT query is still executing even if the image field name is left blank. Try adding your INSERT query inside the first if statement.

    PHP Code:
    if ($_FILES['img_exterior']['tmp_name'] != "") { 

       if (
    $_FILES['img_exterior']['type'] == "image/gif" or $_FILES['img_exterior']['type'] == "image/pjpeg"){ 
    move_uploaded_file($_FILES['img_exterior']['tmp_name'], "images/".$_FILES['img_exterior']['name']) 
        or die (
    "Could not copy"); } 
       else { 

                echo 
    "Could Not Copy, Wrong Filetype (".$_FILES['img_exterior']['name'].")"

            } 

    // INSERT/UPDATE query and everything else related to uploading image here

    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine

  5. #5
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    Greenville, SC
    Posts
    388
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can see how that would work, problem is I have 3 file upload fields. :

    PHP Code:
    <?php
     
     
    //Update photos if any.
     
    if ($_FILES['img_exterior']['tmp_name'] != "") { 
    if (
    $_FILES['img_exterior']['type'] == "image/gif" or $_FILES['img_exterior']['type'] == "image/pjpeg"){ 
    move_uploaded_file($_FILES['img_exterior']['tmp_name'], "images/".$_FILES['img_exterior']['name']) 
        or die (
    "Could not copy"); }
    else { 
              
                echo 
    "Could Not Copy, Wrong Filetype (".$_FILES['img_exterior']['name'].")"
       
            } 
      }
    //}    
    //interior upload
    if ($_FILES['img_interior']['tmp_name'] != "") { 
    if (
    $_FILES['img_interior']['type'] == "image/gif" or $_FILES['img_interior']['type'] == "image/pjpeg"){ 
    move_uploaded_file($_FILES['img_interior']['tmp_name'], "images/".$_FILES['img_interior']['name']) 
        or die (
    "Could not copy"); }
    else { 
              
                echo 
    "Could Not Copy, Wrong Filetype (".$_FILES['img_interior']['name'].")"
       
            } 
      }
    //}    
    if ($_FILES['img_cockpit']['tmp_name'] != "") { 
    if (
    $_FILES['img_cockpit']['type'] == "image/gif" or $_FILES['img_cockpit']['type'] == "image/pjpeg"){ 
    move_uploaded_file($_FILES['img_cockpit']['tmp_name'], "images/".$_FILES['img_cockpit']['name']) 
        or die (
    "Could not copy"); }
    else { 
              
                echo 
    "Could Not Copy, Wrong Filetype (".$_FILES['img_cockpit']['name'].")"
       
            } 
    }
     
     
    $sql 
                UPDATE 
                    ac_description 
                SET 
                    description_order     = '
    $description_order', 
                    actype_ID             = '
    $typeID', 
                    ac_name               = '
    $ac_name', 
                    serial_num            = '
    $serial_num', 
                    registration_num      = '
    $registration_num', 
                    L_engine              = '
    $L_engine', 
                    R_engine              = '
    $R_engine', 
                    L_propeller           = '
    $L_propeller', 
                    R_propeller           = '
    $R_propeller', 
                    C_propeller           = '
    $C_propeller', 
                    interior              = '
    $interior', 
                    exterior              = '
    $exterior', 
                    optional_equip        = '
    $optional_equip', 
                    avionics              = '
    $avionics', 
                    img_exterior          = '"
    .$_FILES['img_exterior']['name']."', 
                    img_interior          = '"
    .$_FILES['img_interior']['name']."', 
                    img_cockpit           = '"
    .$_FILES['img_cockpit']['name']."'
                WHERE 
                    ID                    = '
    $ac_desc_ID'"
     
     
     
     
      
      
    if(
    mysql_query($sql)) {
    echo(
    'The aircraft has been edited');
    }
    else {
    echo(
    'error editing chapter:' mysql_error());
    }
    }
    ?>
    how would that method be used on this page with three file uploads?

  6. #6
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I also have a section with three upload fields. However, I use three different UPDATE queries for those three upload fields, instead of one single query.

    In your case, it would be something like...

    PHP Code:
    //Update photos if any.
    if ($_FILES['img_exterior']['tmp_name'] != "") { 
    if (
    $_FILES['img_exterior']['type'] == "image/gif" or $_FILES['img_exterior']['type'] == "image/pjpeg"){ 
    move_uploaded_file($_FILES['img_exterior']['tmp_name'], "images/".$_FILES['img_exterior']['name']) 
        or die (
    "Could not copy"); }
    else { 
              
                echo 
    "Could Not Copy, Wrong Filetype (".$_FILES['img_exterior']['name'].")"
       
            } 

    $sql 
                UPDATE 
                    ac_description 
                SET
                    img_exterior          = '"
    .$_FILES['img_exterior']['name']."'
                WHERE 
                    ID                    = '
    $ac_desc_ID'";

    mysql_query($sql)) or die();
      } 
    As I said, it may not be the best solution but this is all I could come up with.
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine


Bookmarks

Posting Permissions

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