PHP: $_FILES not working

a2jfreaka2jfreak Houston, TX Member
edited June 2004 in Internet & Media
[php]
HTML:
<input type="file" name="a" size="30" />
<input type="file" name="b" size="30" />
<input type="file" name="c" size="30" />

PHP:
echo "=======\$_POST variables=======<br>\n";
foreach ($_POST as $key => $value) {
echo "$key\t= $value<br>\n";
}

echo "<br><br>=======\$_FILES variables=======<br>\n";
foreach ($_FILES as $key => $value) {
echo "$key\t= $value<br>\n";
}
[/php]

prints:
=======$_POST variables=======
a = abc.jpg
b = xyz.jpg
c = 123.jpg

=======$_FILES variables=======

Notice how the FILE input elements show up in $_POST and not $_FILES?
Any idea what's wrong? I know I must have something wrong (and probably something stupid) but I don't know what.

Comments

  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited June 2004
    Two reasons. First, $_FILES is a multidimensional array, so typical $k=>$v won't work. Second, I'm almost positive $_FILES won't work unless you let the form know you're adding file upload. Try adding the following to your form:
    &lt;form enctype="multipart/form-data" name="add" method="post" action="youraction.php">
    &lt;input type="hidden" name="MAX_FILE_SIZE" value="3145728" />
    

    Next, for displaying the results, because $_FILES is multidimensional, you're going to need a foreach in your foreach:

    [php]
    echo "=======\$_FILES variables=======\n";
    foreach ($_FILES as $key => $value) {
    foreach ($value as $value2) {
    echo "$key\t= $value2\n<br />";
    }
    }[/php]

    The problem is that it's going to display each value of the array. I'm not sure how to display just the filename.
  • a2jfreaka2jfreak Houston, TX Member
    edited June 2004
    You're correct, it is multi-dimensional. I just had the key/value there to have it iterate through the names. The way to make it show the filename and other stuff is like:
    [php]
    $attachment = $_FILES[$file];
    $attachment_name = $_FILES[$file];
    [/php]

    The enctype is what was wrong . . . thanks!
    Mr. Kwitko wrote:
    Two reasons. First, $_FILES is a multidimensional array, so typical $k=>$v won't work. Second, I'm almost positive $_FILES won't work unless you let the form know you're adding file upload. Try adding the following to your form:
    &lt;form enctype="multipart/form-data" name="add" method="post" action="youraction.php">
    &lt;input type="hidden" name="MAX_FILE_SIZE" value="3145728" />
    

    Next, for displaying the results, because $_FILES is multidimensional, you're going to need a foreach in your foreach:

    [php]
    echo "=======\$_FILES variables=======\n";
    foreach ($_FILES as $key => $value) {
    foreach ($value as $value2) {
    echo "$key\t= $value2\n<br />";
    }
    }[/php]

    The problem is that it's going to display each value of the array. I'm not sure how to display just the filename.
Sign In or Register to comment.