Jump to content

Functionality of the $_FILES array


vineld

Recommended Posts

I have seen plenty of examples, even in the php manual, where they only check to see if the $_FILES['whatevermynameis'] array is empty or not set although this do not seem to work properly. I often encounter that the array is always set after the form has been submitted no matter if a file has been uploaded or not so I normally check size instead. Why is this? What is actually the "correct" way to check if a file has been selected?

Link to comment
Share on other sites

If you see this example from php.net you'll see that you can check the error array key of the uploaded file(s)

 

<?php
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, "data/$name");
    }
}
?>

Link to comment
Share on other sites

Yeah, I have used that as well although if it's possible that the $_FILES['myname'] array is empty there will be no error either. Will the look of the array depend on the browser or is it php that decides? I think I still have PHP4 versions running in some places...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.