SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    UK
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_FILES and if {} else {} statement

    Hi all
    I have been trying to run a form with a FILE field through an upload script I have written

    If the user decided not to include a file the form must be prcessed through on part of the script, and if they did through another. I have tried too run this through several if else statements, but the form is processed through one or the other depedant on state.

    Below are some of the options I tried.

    PHP Code:
    if (!isset($_FILES['fieldname'])){

    Do 
    this
    }
    else

    Do 
    this
    }


    if (!isset(
    $_POST['fieldname'])){

    Do 
    this
    }
    else

    Do 
    this
    }


    if (
    $_FILES['fieldname'] = 0){

    Do 
    this
    }
    else

    Do 
    this
    }

    if (
    $_POST['fieldname'] = 0){

    Do 
    this
    }
    else

    Do 
    this
    }


    if (
    $_FILES['fieldname'] = 0){

    Do 
    this
    }
    else if (
    $_FILES['fieldname'] > 0){
     
    Do 
    this
    }



    if (!isset(
    $_FILES['fieldname'] )){

    Do 
    this
    }
    else if (isset(
    $_FILES['fieldname'] )){
     
    Do 
    this

    I can make the script go to one or the other options easily, but it fails to acknowledge both depedant upon an empty field. Am I barking up the worng tree here with regards $_FILES ?

    All help appreciated, and thatnks are in advance.

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't try to guess it.
    Just find out what you have

    var_dump($_FILES);
    var_dump($_POST);

  3. #3
    <? james('rules'); ?>
    Join Date
    Jun 2004
    Location
    Wales, UK
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(isset($_FILE['fieldname'])){

    if(isset(
    $_POST['fieldname'])) {

    // do something here

    else {

    // if one or bothwere blank do this..


    If knowledge is power - Why isn't our army librarians?!
    Statistics show that 63% of all statistics are fake.
    When i was little i broke my neck, and i havent looked back since .
    I completed the internet in 1 week. The end boss was pretty easy though .

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    UK
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(isset($_FILE['fieldname'])){ 

    if(isset(
    $_POST['fieldname'])) { 

    // do something here 

    else { 

    // if one or bothwere blank do this.. 


    Well at a first glance this code has an error in $_FILE should be $_FILES and also it looks unbalanced with {}. I count four not six. Thanks for your suggestion, but I have tried this approach.

    PHP Code:
    if(isset($_FILES['fieldname'])){ 

    if(isset(
    $_POST['fieldname'])) { 

    // do something here 
        
    }
    }
    else { 

    // if one or bothwere blank do this.. 


    Here's a re-cycled version.

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A complete example

    PHP Code:
    <form action="" method="post" enctype="multipart/form-data">
    <input type="text" name="text_field" value="text_field_value"><br>
    <input type="file" name="file_field"><br>
    <input type="submit">
    </form>

    <h2>$_POST  contains</h2><pre><?php var_dump($_POST); ?></pre>
    <h2>$_FILES contains</h2><pre><?php var_dump($_FILES); ?></pre>
    Instructions:
    -- cut'n'paste, save as .php, open in browser
    -- click submit firstly without taking a file, then with some file selected, compare results

    As you will see, $_FILES is always populated, no matter if you've actually selected anything. Ergo, isset makes no sense, consider other methods.

    Documentation: http://us2.php.net/manual/en/features.file-upload.php

  6. #6
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Was is_uploaded_file not an option?
    if (is_uploaded_file($_FILES['file_field']))


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
  •