#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    1

    Submitting a form without choosing an image


    I'd like to give a user the chance to submit the form information without having to choose an image. However, if they don't submit an image, the page just refreshes without sending any information to the database. I'm not really sure how to setup a check to see if an image was selected or not and to continue with sending the form if an image wasn't selected. I've check Google and there is plenty of information for doing the opposite of what I want to do, where someone wants to submit an image without the form but haven't found any solid information on how to do it the other way around. Thanks for your time.

    upload.php

    PHP Code:
    if (isset($_FILES['image'], $_POST['name'], $_POST['storedrop'], $_POST['description'], $_POST['description2'])) {
        
    $image_name $_FILES['image']['name'];
        
    $image_size $_FILES['image']['size'];
        
    $image_temp $_FILES['image']['tmp_name'];
            
        
    $allowed_ext = array('jpeg''jpg''gif''png');
        
    $image_ext strtolower(end(explode('.'$image_name)));
        
        
    $name $_POST['name'];
        
    $description $_POST['description'];
        
    $description2 $_POST['description2'];
        
    $storedrop $_POST['storedrop'];
        
        
    $errors = array();

        if (
    $storedrop == 'store1') {
            
    $store 'store1';
        }
        
        if (
    $storedrop == 'store2') {
            
    $store 'store2';
        }
        
        if (empty(
    $image_name)) {
            
    $errors[] = 'Please Wait';
        } else {
            if (
    in_array($image_ext$allowed_ext) === false) {
                    
    $errors[] = 'File type is not allowed';
            }
        }
        
        if (empty(
    $name) || empty($description) || empty($store)) {
            
    $errors[] = 'Name, description and store are required!';
        }
        
        if (!empty(
    $errors)) {
            foreach (
    $errors as $error) {
                echo 
    '<span style"color: red;">'$error'</span><br />';
            }
        } else {
            
    $user_id $_SESSION['user_id'];

            function 
    upload_special($image_temp$image_ext$user_id$name$description$description2$store) {
                
    $user_id = (int)$user_id;
                
    $name mysql_real_escape_string($name);
                
    $description mysql_real_escape_string($description);
                
    $description2 mysql_real_escape_string($description2);
                
    $store mysql_real_escape_string($store);
        
                
    mysql_query("INSERT INTO `specialspage` VALUES ('', '$user_id', UNIX_TIMESTAMP(), '$name', '$store', '$image_ext', '$description', '$description2')");
        
                
    $id mysql_insert_id();
                
    $image_file $id.'.'.$image_ext;
                
    move_uploaded_file($image_temp'specials/'.$image_file);
        
                
    create_thumb_specials('specials/'$image_file'specials/thumbs/');
            }
            
            
    upload_special($image_temp$image_ext$user_id$name$description$description2$store);
            
    header('Location: specialschange.php');
            exit();
        }
    }

    ?> 
  2. #2
  3. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    501
    Rep Power
    127
    Well, for starters you're requiring them to:

    isset($_FILES['image']...
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    101
    Rep Power
    26
    The funny part of that is, I thought that isset($_FILES['foo']) where "foo" is the name of your <input>, would always return TRUE? The element of $_FILES wille exist, but the things in $_FILES['foo'] will be empty (except for "error" which would be set to UPLOAD_ERR_NO_FILE ).
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    1
    So I changed the code, but it doesn't seem to recognize the FILES['image'] as being empty, not sure what's going wrong there.

    PHP Code:
    if (isset($_FILES['image'], $_POST['name'], $_POST['storedrop'], $_POST['description'], $_POST['description2'])) {
        
        
    $errors = array();
        
        if (empty(
    $_FILES['image'])) {
            
    $image_temp '';
            
    $image_ext 'empt';
        } else {
            
    $image_name $_FILES['image']['name'];
            
    $image_size $_FILES['image']['size'];
            
    $image_temp $_FILES['image']['tmp_name'];
            
            
    $allowed_ext = array('jpeg''jpg''gif''png');
            
    $image_ext strtolower(end(explode('.'$image_name)));
            
            if (empty(
    $image_name)) {
                
    $errors[] = 'Please Wait';
            } else {
                if (
    in_array($image_ext$allowed_ext) === false) {
                    
    $errors[] = 'File type is not allowed';
                }
            }
        }
        
        
    $name $_POST['name'];
        
    $description $_POST['description'];
        
    $description2 $_POST['description2'];
        
    $storedrop $_POST['storedrop'];
        
        if (
    $storedrop == 'store1') {
            
    $store 'store1';
        }
        
        if (
    $storedrop == 'store2') {
            
    $store 'store2';
        }

        
        if (empty(
    $name) || empty($description) || empty($store)) {
            
    $errors[] = 'Name, description and store are required!';
        }
        
        if (!empty(
    $errors)) {
            foreach (
    $errors as $error) {
                echo 
    '<span style"color: red;">'$error'</span><br />';
            }
        } else {
            
    $user_id $_SESSION['user_id'];

            function 
    upload_special($image_temp$image_ext$user_id$name$description$description2$store) {
                
    $user_id = (int)$user_id;
                
    $name mysql_real_escape_string($name);
                
    $description mysql_real_escape_string($description);
                
    $description2 mysql_real_escape_string($description2);
                
    $store mysql_real_escape_string($store);
        
                
    mysql_query("INSERT INTO `specialspage` VALUES ('', '$user_id', UNIX_TIMESTAMP(), '$name', '$store', '$image_ext', '$description', '$description2')");
        
                if (
    $image_ext !== 'empt') {
                    
    $id mysql_insert_id();
                    
    $image_file $id.'.'.$image_ext;
                    
    move_uploaded_file($image_temp'specials/'.$image_file);
        
                    
    create_thumb_specials('specials/'$image_file'specials/thumbs/');
                }
            }
            
            
    upload_special($image_temp$image_ext$user_id$name$description$description2$store);
            
    header('Location: specialschange.php');
            exit();
        }
    }

    ?> 
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    1
    Originally Posted by mod_speling
    The funny part of that is, I thought that isset($_FILES['foo']) where "foo" is the name of your <input>, would always return TRUE? The element of $_FILES wille exist, but the things in $_FILES['foo'] will be empty (except for "error" which would be set to UPLOAD_ERR_NO_FILE ).
    So, how would I do it so the form doesn't worry about anything to do with the file if no file was chosen and just upload the form
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    1
    I got it to work by removing these checks,

    if (empty($image_name)) {
    $errors[] = 'Please Wait';
    } else {
    if (in_array($image_ext, $allowed_ext) === false) {
    $errors[] = 'File type is not allowed';
    }
    }
  12. #7
  13. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    501
    Rep Power
    127
    Originally Posted by mod_speling
    The funny part of that is, I thought that isset($_FILES['foo']) where "foo" is the name of your <input>, would always return TRUE? The element of $_FILES wille exist, but the things in $_FILES['foo'] will be empty (except for "error" which would be set to UPLOAD_ERR_NO_FILE ).
    I'd have to test it to see but I'd doubt $_FILES is populated if there's not an actual file. $_POST["foo"] might be set and empty, though.

    But I could be wrong. Been a while since I dealt with an optional file upload field.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    101
    Rep Power
    26
    I did just check on 5.3.
    PHP Code:
    <?php
    if (isset($_FILES['userfile'])) {
        
    print_r($_FILES); exit;
    }
    ?>
    <form method="post" enctype="multipart/form-data">
    <input type="file" name="userfile"/>
    <input type="submit"/>
    </form>
    $_FILES['userfile'] is always set.

    OP I think your checks should be:
    * if $_FILES['image']['name'] is not empty; AND $_FILES['image']['error'] is UPLOAD_ERR_OK (or empty); then the user is trying to upload a file.
    * to check for further errors you could valdate the file's extension, and always check using is_uploaded_file() or move_uploaded_file() that the file in question is a valid upload before moving it to another location on the server.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    1
    Ok I'll try that because when echoing out the data it still shows a square with a question mark where an image should be, ill post back on this thread if I still run into an issue. Thanks
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    1
    So I changed the code to this,

    if (!empty($_FILES['image']['name']) && $_FILES['image']['error'] == UPLOAD_ERROR_OK) {
    $image_name = $_FILES['image']['name'];
    $image_size = $_FILES['image']['size'];
    $image_temp = $_FILES['image']['tmp_name'];

    $allowed_ext = array('jpeg', 'jpg', 'gif', 'png');
    $image_ext = strtolower(end(explode('.', $image_name)));
    }

    But the square with a question mark still shows up when echoing the row after form submission.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    1
    nvm it had something to do with how I was echoing the data. fixed the square

IMN logo majestic logo threadwatch logo seochat tools logo