$_FILES

$HTTP_POST_FILES [deprecated]

(PHP 4 >= 4.1.0, PHP 5)

$_FILES -- $HTTP_POST_FILES [deprecated]HTTP File Upload variables

Description

An associative array of items uploaded to the current script via the HTTP POST method.

$HTTP_POST_FILES contains the same initial information, but is not a superglobal. (Note that $HTTP_POST_FILES and $_FILES are different variables and that PHP handles them as such)

Changelog

Version Description
4.1.0 Introduced $_FILES that deprecated $HTTP_POST_FILES.

Notes

Note:

This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.

See Also

Коментарии

If $_FILES is empty, even when uploading, try adding enctype="multipart/form-data" to the form tag and make sure you have file uploads turned on.
2009-01-17 02:16:26
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
The format of this array is (assuming your form has two input type=file fields named "file1", "file2", etc):

Array
(
    [file1] => Array
        (
            [name] => MyFile.txt (comes from the browser, so treat as tainted)
            [type] => text/plain  (not sure where it gets this from - assume the browser, so treat as tainted)
            [tmp_name] => /tmp/php/php1h4j1o (could be anywhere on your system, depending on your config settings, but the user has no control, so this isn't tainted)
            [error] => UPLOAD_ERR_OK  (= 0)
            [size] => 123   (the size in bytes)
        )

    [file2] => Array
        (
            [name] => MyFile.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/php/php6hst32
            [error] => UPLOAD_ERR_OK
            [size] => 98174
        )
)

Last I checked (a while ago now admittedly), if you use array parameters in your forms (that is, form names ending in square brackets, like several file fields called "download[file1]", "download[file2]" etc), then the array format becomes... interesting.

Array
(
    [download] => Array
        (
            [name] => Array
                (
                    [file1] => MyFile.txt
                    [file2] => MyFile.jpg
                )

            [type] => Array
                (
                    [file1] => text/plain
                    [file2] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [file1] => /tmp/php/php1h4j1o
                    [file2] => /tmp/php/php6hst32
                )

            [error] => Array
                (
                    [file1] => UPLOAD_ERR_OK
                    [file2] => UPLOAD_ERR_OK
                )

            [size] => Array
                (
                    [file1] => 123
                    [file2] => 98174
                )
        )
)

So you'd need to access the error param of file1 as, eg $_Files['download']['error']['file1']
2009-03-18 12:35:46
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
For some reason when I tried to check if $_FILES['myVarName'] was empty() or !isset() or array_key_exists(), it always came back that the file was indeed in the superglobal, even when nothing was uploaded.

I wonder if this is a result of enctype="multipart/form-data".

Anyways, I solved my issue by checking to make sure that $_FILES['myVarName']['size'] > 0
2009-06-29 19:51:37
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
To determine whether upload was successful you should check for error being UPLOAD_ERR_OK instead of checking the file size. When nothing is chosen to be uploaded, the key in $_FILES will still be there, but it should have error equal UPLOAD_ERR_NO_FILE.
2009-08-13 08:40:55
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Автор:
In the past you could unconditionally call $_FILES['profile_pic'] without ever having to worry about PHP spitting an "Undefined index: profile_pic" error (so long as the page posting had a file input on it (e.g. <input type="file" name="profile_pic" />)). This was the case regardless of whether or not the end user actually uploaded a file. These days, with so many people browsing the web via iPads, you have to explicitly check to see if the input isset($_FILES['profile_pic']) before calling into it, else you'll get the aforementioned error message. This is because iOS devices running Safari disable file inputs thereby causing them to be treated as if they don't exist. Time to update your scripts!

-john
2011-02-03 20:14:13
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Автор:
As mentioned , you should check the error index of the upload.

Example below suggests you have a file field named 'image'.

<?php

       
if($_FILES['image']['error'] == 0){
           
// success - move uploaded file and process stuff here

       
}else{
           
// 'there was an error uploading file' stuff here....   
       
}
?>
2011-04-18 09:23:17
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Автор:
Having url rewrite patterns in .htaccess file which modify your urls can affect $_FILES sometimes. Even though the php page loads and works fine, this variable may not work because of it. Therefore if you rewrite 'www.example.com' to 'example.com', make sure you use the latter one when sending POST to the php page. I'm still not sure why this happens, but its worth noting here so others don't spend time chasing ghosts.
2011-04-26 17:48:45
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Автор:
If you're uploading multiple files and you name your file inputs "upload[]" the $_FILES array will look different than the var_dump posted below. I figured I'd post what it looks like since it caused me (and still causes me) headaches!

array(1) {
    ["upload"]=>array(5) {
        ["name"]=>array(3) {
            [0]=>string(9)"file0.txt"
            [1]=>string(9)"file1.txt"
            [2]=>string(9)"file2.txt"
        }
        ["type"]=>array(3) {
            [0]=>string(10)"text/plain"
            [1]=>string(10)"text/plain"
            [2]=>string(10)"text/plain"
        }
        ["tmp_name"]=>array(3) {
            [0]=>string(14)"/tmp/blablabla"
            [1]=>string(14)"/tmp/phpyzZxta"
            [2]=>string(14)"/tmp/phpn3nopO"
        }
        ["error"]=>array(3) {
            [0]=>int(0)
            [1]=>int(0)
            [2]=>int(0)
        }
        ["size"]=>array(3) {
            [0]=>int(0)
            [1]=>int(0)
            [2]=>int(0)
        }
    }
}

(I thought the array would have looked like upload[index][name] which is not the case.)
2011-06-13 10:36:52
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
In checking the error code, you probably ought to check for code 4.  I believe Code 4 means no file was uploaded, and there are many instances where that's perfectly OK.

Such as when you have a form with multiple data items, including file and image uploads, plus whatever else.  The user might not be adding a new upload for whatever reason, such as there may already be a file in the system from an earlier update, and the user is satisfied with that.
2011-09-02 16:31:26
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
For situation download[file1], download[file2], ..., download[fileN], try it:

<?php

/**
 *
 * @param array     $arrayForFill
 * @param string    $currentKey
 * @param mixed     $currentMixedValue
 * @param string    $fileDescriptionParam (name, type, tmp_name, error или size)
 * @return void
 */
function rRestructuringFilesArray(&$arrayForFill$currentKey$currentMixedValue$fileDescriptionParam)
{
    if (
is_array($currentMixedValue)) {
        foreach (
$currentMixedValue as $nameKey => $mixedValue) {
           
rRestructuringFilesArray($arrayForFill[$currentKey],
                                     
$nameKey,
                                     
$mixedValue,
                                     
$fileDescriptionParam);
        }
    } else {
       
$arrayForFill[$currentKey][$fileDescriptionParam] = $currentMixedValue;
    }
}

$arrayForFill = array();
foreach (
$_FILES as $firstNameKey => $arFileDescriptions) {
    foreach (
$arFileDescriptions as $fileDescriptionParam => $mixedValue) {
       
rRestructuringFilesArray($arrayForFill,
                                 
$firstNameKey,
                                 
$_FILES[$firstNameKey][$fileDescriptionParam],
                                 
$fileDescriptionParam);
    }
}
$_FILES $arrayForFill;
?>
2011-11-18 06:39:53
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Nontypicall array comes in php after the submission.I wrote a small function to restate it to the familiar look.
<?php
function multiple(array $_files$top TRUE)
{
   
$files = array();
    foreach(
$_files as $name=>$file){
        if(
$top$sub_name $file['name'];
        else   
$sub_name $name;
       
        if(
is_array($sub_name)){
            foreach(
array_keys($sub_name) as $key){
               
$files[$name][$key] = array(
                   
'name'     => $file['name'][$key],
                   
'type'     => $file['type'][$key],
                   
'tmp_name' => $file['tmp_name'][$key],
                   
'error'    => $file['error'][$key],
                   
'size'     => $file['size'][$key],
                );
               
$files[$name] = multiple($files[$name], FALSE);
            }
        }else{
           
$files[$name] = $file;
        }
    }
    return 
$files;
}

print_r($_FILES);
/*
Array
(
    [image] => Array
        (
            [name] => Array
                (
                    [0] => 400.png
                )
            [type] => Array
                (
                    [0] => image/png
                )
            [tmp_name] => Array
                (
                    [0] => /tmp/php5Wx0aJ
                )
            [error] => Array
                (
                    [0] => 0
                )
            [size] => Array
                (
                    [0] => 15726
                )
        )
)
*/
$files multiple($_FILES);
print_r($files);
/*
Array
(
    [image] => Array
        (
            [0] => Array
                (
                    [name] => 400.png
                    [type] => image/png
                    [tmp_name] => /tmp/php5Wx0aJ
                    [error] => 0
                    [size] => 15726
                )
        )
)
*/
?>
2011-11-22 00:51:50
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
THis is an solution to convert Cyrillic and umlaut characters as file name when uplaoding files into needed encoding. Was searching for it but could not find. Thus posting this. Just like this:

$value = mb_convert_encoding($value, "UTF-8");
2012-03-15 18:49:14
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Автор:
You can check error index this way:

<?php

$errorIndex 
$_FILES["file"]["error"];

if (
$errorIndex 0) {
    die(
'We have a error. Try Again.');
}

processFile();

?>
2012-05-20 07:31:17
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
I just spent long time debugging strange behavior of one of our application on new webhosting. We have 30 file inputs on one page for upload to server. Problem was that only 20 was actually uploaded.

Now I found there is an option max_file_uploads in php.ini limiting maximum size of $_FILES to 20 by default.

When you have suhosin extension installed it has own option limiting same thing to 25 (suhosin.upload.max_uploads in php.ini)
2012-07-02 17:02:31
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Автор:
For quick debugging (eg. var_dump($_FILES);), these are the values of the error constants. Obviously don't use these for comparison in real code.

UPLOAD_ERR_OK: 0
UPLOAD_ERR_INI_SIZE: 1
UPLOAD_ERR_FORM_SIZE: 2
UPLOAD_ERR_NO_TMP_DIR: 6
UPLOAD_ERR_CANT_WRITE: 7
UPLOAD_ERR_EXTENSION: 8
UPLOAD_ERR_PARTIAL: 3
2012-08-05 00:46:11
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
A note of security: Don't ever trust $_FILES["image"]["type"]. It takes whatever is sent from the browser, so don't trust this for the image type.  I recommend using finfo_open (function.finfo-open) to verify the MIME type of a file. It will parse the MAGIC in the file and return it's type...this can be trusted (you can also use the "file" program on Unix, but I would refrain from ever making a System call with your PHP code...that's just asking for problems).
2012-09-01 23:59:21
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
A nice trick to reorder the $_FILES array when you use a input name as array is:

<?php
function diverse_array($vector) {
   
$result = array();
    foreach(
$vector as $key1 => $value1)
        foreach(
$value1 as $key2 => $value2)
           
$result[$key2][$key1] = $value2;
    return 
$result;
}
?>

will transform this:

array(1) {
    ["upload"]=>array(2) {
        ["name"]=>array(2) {
            [0]=>string(9)"file0.txt"
            [1]=>string(9)"file1.txt"
        }
        ["type"]=>array(2) {
            [0]=>string(10)"text/plain"
            [1]=>string(10)"text/html"
        }
    }
}

into:

array(1) {
    ["upload"]=>array(2) {
        [0]=>array(2) {
            ["name"]=>string(9)"file0.txt"
            ["type"]=>string(10)"text/plain"
        },
        [1]=>array(2) {
            ["name"]=>string(9)"file1.txt"
            ["type"]=>string(10)"text/html"
        }
    }
}

just do:

<?php $upload diverse_array($_FILES["upload"]); ?>
2012-09-05 23:09:33
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
I realize there are a number of posts here for reformating the php $_FILES array, but they don't handle all cases.  This handles the single case, the multiple file case, and even submitting multiple file arrays.  This way no matter what, before ever touching the files array I call this regardless of what it might be:

<?php
   
/**
     * This is to fix the odd files array PHP creates when a file input has a name that's php array:
     * eg: <file name="model[column]" value='file1'> <file name="model[column2][]" value='file2'>
     * becomes:  $_FILES['model']['name'][column] = file1_name.xxx
     *           $_FILES['model']['name'][column2][0] = file2_name.xxx
     *
     * this changes it to:
     *           $files['model'][column]['name'] = file1_name.xxx
     *           $files['model'][column2][0]['name'] = file2_name.xxx
     *
     * This way the file data is grouped together as expected and as it does with a non-array type name attribute
     */
   
static public function multi_file_fix($files null)
    {

        if (
$files == null) {
           
$files = (is_array($_FILES)) ? $_FILES : array();
        }

       
//make there there is a file, and see if the first item is also an array
       
$new_files = array();
        foreach (
$files as $name => $attributes) {
            if (
is_array(reset($attributes))) { //check first item
               
foreach ($attributes as $attribute => $item) { //array file submit, eg name="model[file]"
                   
foreach ($item as $key => $value) {
                        if (
is_array($value)) {
                            foreach (
$value as $key2 => $sub_val) { // multi-array file submit, eg name="model[file][]"
                               
$new_files[$name][$key][$key2][$attribute] = $sub_val;
                            }
                        } else {
                           
$new_files[$name][$key][$attribute] = $value;
                        }
                    }
                }
            } else { 
// regular file submit, eg name="file"
               
$new_files[$name] = $attributes;
            }
        }

        return 
$new_files;
    }

//Usage:

$files multi_file_fix($_FILES);

?>
2013-10-09 03:51:30
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Others have posted about how the $_FILES array organizes data differently depending on whether the HTML input is a single or multiple type, so it seems to be a common enough problem.  If for some reason you need to mix-and-match the types, or you're not sure which how many files you'll be expecting from a multiple input, this is a very useful way to reorganize the $_FILES array.  Also, unlike some of the earlier posts, the formatting of the new array (i.e. the number of keys and values) is consistent.

<?php
// Reorganize $_FILES array information
$files = Array ();
$i 0;

// Start with all inputs in $_FILES array
foreach ($_FILES as $input)
{
   
$j 0;
   
    foreach (
$input as $property => $value)
    {
        if (
is_array($value))
        {
           
$j count($value); // Number of iterations
           
           
for ($k 0$k $j; ++$k)
            {
               
$files[$i $k][$property] = $value[$k];
            }
        }
        else
        {
           
$j 1;
           
           
$files[$i][$property] = $value;
        }
    }
   
   
$i += $j;
}
?>

The results will look something like this:
$files = Array (
   [0] => Array (
      [name] => '' 
      [type] => ''
      [tmp_name] => ''
      [error] => 0
      [size] => 0 
   )
)
2014-01-10 03:27:12
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Here is my version of $_FILES rearrange. Unlike other codes here, this working well on any depth of $_FILES.
<?php
if (!empty($_FILES)) {
    function 
rearrange_files_array(array $array) {
        foreach (
$array as &$value) {
           
$_array = array();
            foreach (
$value as $prop => $propval) {
                if (
is_array($propval)) {
                   
array_walk_recursive($propval, function(&$item$key$value) use($prop) {
                       
$item = array($prop => $item);
                    }, 
$value);
                   
$_array array_replace_recursive($_array$propval);
                } else {
                   
$_array[$prop] = $propval;
                }
            }
           
$value $_array;
        }
        return 
$array;
    }
    echo 
'<pre>'.print_r(rearrange_files_array($_FILES), true).'</pre>';
}
?>
<form method="post" enctype="multipart/form-data" style="clear: both;">
    <h3>upload1</h3>
    <div><label>new0</label><input type="file" name="upload1[new][]" /></div>
    <div><label>new1</label><input type="file" name="upload1[new][]" /></div>
    <div><label>update.id11</label><input type="file" name="upload1[update][id11]" /></div>
    <div><label>update.id12</label><input type="file" name="upload1[update][id12]" /></div>
    <hr />
    <h3>upload2</h3>
    <div><label>new0</label><input type="file" name="upload2[]" /></div>
    <div><label>new1</label><input type="file" name="upload2[]" /></div>
    <div><label>update.id21</label><input type="file" name="upload2[id21]" /></div>
    <div><label>update.id22</label><input type="file" name="upload2[id22]" /></div>
    <hr />
    <div><label>upload3</label><input type="file" name="upload3" /></div>
    <input type="submit" value="go" />
</form>

The output after empty form is posted:
Array
(
    [upload1] => Array
        (
            [new] => Array
                (
                    [0] => Array
                        (
                            [name] => 
                            [type] => 
                            [tmp_name] => 
                            [error] => 4
                            [size] => 0
                        )
                    [1] => Array
                        (
                            [name] => 
                            [type] => 
                            [tmp_name] => 
                            [error] => 4
                            [size] => 0
                        )
                )
            [update] => Array
                (
                    [id11] => Array
                        (
                            [name] => 
                            [type] => 
                            [tmp_name] => 
                            [error] => 4
                            [size] => 0
                        )
                    [id12] => Array
                        (
                            [name] => 
                            [type] => 
                            [tmp_name] => 
                            [error] => 4
                            [size] => 0
                        )
                )
        )
    [upload2] => Array
        (
            [0] => Array
                (
                    [name] => 
                    [type] => 
                    [tmp_name] => 
                    [error] => 4
                    [size] => 0
                )
            [1] => Array
                (
                    [name] => 
                    [type] => 
                    [tmp_name] => 
                    [error] => 4
                    [size] => 0
                )
            [id21] => Array
                (
                    [name] => 
                    [type] => 
                    [tmp_name] => 
                    [error] => 4
                    [size] => 0
                )
            [id22] => Array
                (
                    [name] => 
                    [type] => 
                    [tmp_name] => 
                    [error] => 4
                    [size] => 0
                )
        )
    [upload3] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )
)
2014-03-10 18:29:35
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
If you are looking for the $_FILES['error'] code explanations, be sure to read:

Handling File Uploads - Error Messages Explained
features.file-upload.errors
2014-03-16 08:11:39
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
I spent 3 hours trying to find out why when I upload multiples file $_FILES  return empty, I did noticed it was only when I select files that exceed 3m so I thought it was something related to the MAX_UPLOAD_SIZE that for my surprice came as default as 20m which was very confusing. Later I discovery the problem was in the POST_MAX_SIZE been 3m, so it happen that not only MAX_UPLOAD_SIZE is responsible and that is why I'd like to know there is no error message that shows the cause.
2014-06-06 19:18:00
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
see features.file-upload.post-method for documentation of the $_FILES array, which is what I came to this page for in the first place.
2014-11-14 23:49:01
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
If you are getting NULL values and want to see what error is being returned you can add ' 2>&1' to the end of your command. On a linux server this will redirect the stderr to stdout (so the string error will be output). This probably saved me a ton of time.
2015-07-14 15:46:35
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Автор:
I've written this function to restructure deeply nested $_FILES arrays, so that the parameters for each file are grouped together.

function restructure_files(array $input)
{
    $output = [];
    foreach ($input as $name => $array) {
        foreach ($array as $field => $value) {
            $pointer = &$output[$name];
            if (!is_array($value)) {
                $pointer[$field] = $value;
                continue;
            }
            $stack = [&$pointer];
            $iterator = new \RecursiveIteratorIterator(
                new \RecursiveArrayIterator($value),
                \RecursiveIteratorIterator::SELF_FIRST
            );
            foreach ($iterator as $key => $value) {
                array_splice($stack, $iterator->getDepth() + 1);
                $pointer = &$stack[count($stack) - 1];
                $pointer = &$pointer[$key];
                $stack[] = &$pointer;
                if (!$iterator->hasChildren()) {
                    $pointer[$field] = $value;                               
                }
            }
        }
    }
    return $output;
}

Turns this:

array (size=2)
  'one' => 
    array (size=5)
      'name' => 
        array (size=1)
          'inner' => 
            array (size=2)
              11 => string 'DM4C2738.jpg' (length=12)
              5 => string 'DM4C2760.jpg' (length=12)
      'type' => 
        array (size=1)
          'inner' => 
            array (size=2)
              11 => string 'image/jpeg' (length=10)
              5 => string 'image/jpeg' (length=10)
      'tmp_name' => 
        array (size=1)
          'inner' => 
            array (size=2)
              11 => string '/private/var/tmp/phploOZRb' (length=26)
              5 => string '/private/var/tmp/phpsFkmIh' (length=26)
      'error' => 
        array (size=1)
          'inner' => 
            array (size=2)
              11 => int 0
              5 => int 0
      'size' => 
        array (size=1)
          'inner' => 
            array (size=2)
              11 => int 1031601
              5 => int 674697
  'two' => 
    array (size=5)
      'name' => string '9ncYySC.jpg' (length=11)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string '/private/var/tmp/phpuG99X9' (length=26)
      'error' => int 0
      'size' => int 882422

Into this:

array (size=2)
  'one' => 
    array (size=1)
      'inner' => 
        array (size=2)
          11 => 
            array (size=5)
              'name' => string 'DM4C2738.jpg' (length=12)
              'type' => string 'image/jpeg' (length=10)
              'tmp_name' => string '/private/var/tmp/phploOZRb' (length=26)
              'error' => int 0
              'size' => int 1031601
          5 => 
            array (size=5)
              'name' => string 'DM4C2760.jpg' (length=12)
              'type' => string 'image/jpeg' (length=10)
              'tmp_name' => string '/private/var/tmp/phpsFkmIh' (length=26)
              'error' => int 0
              'size' => int 674697
  'two' => 
    array (size=5)
      'name' => string '9ncYySC.jpg' (length=11)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string '/private/var/tmp/phpuG99X9' (length=26)
      'error' => int 0
      'size' => int 882422
2015-11-10 14:05:55
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
I normalized the $_FILES array to

[doc] => Array
        (
            [0] => Array
                (
                    [name] => testfile.txt
                    [type] => application/octet-stream
                    [tmp_name] => /tmp/php6wHXmC
                    [error] => 0
                    [size] => 4
                )

            [affe] => Array
                (
                    [name] => testfile.txt
                    [type] => application/octet-stream
                    [tmp_name] => /tmp/phpfiHK2e
                    [error] => 0
                    [size] => 4
                )

        )

<?php
function getFiles() {
   
$result = array();
    foreach(
$_FILES as $name => $fileArray) {
        if (
is_array($fileArray['name'])) {
            foreach (
$fileArray as $attrib => $list) {
                foreach (
$list as $index => $value) {
                   
$result[$name][$index][$attrib]=$value;
                }
            }
        } else {
           
$result[$name][] = $fileArray;
        }
    }
    return 
$result;
}
?>

So yo have always the same structure, even if you post one ore more files:

<?php
//single file
$request->addFileFromLocalFilePath('doc','testfile.txt');

// multiple files
$request->addFileFromLocalFilePath('doc[0]','testfile.txt');
$request->addFileFromLocalFilePath('doc[affe]','testfile.txt');

foreach (
$request->getFiles() as $fieldName => $files) {
    foreach (
$files as $index => $fileArray) {
        echo 
$fileArray['tmp_name'];
    }
}

?>
2017-08-09 15:43:38
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Here's a function that I have used to get a nice simple array of all incoming files from a page. It basically just flattens the $FILES array. This function works on many file inputs on the page and also if the inputs are '<input type="file[]" multiple>'. Note that this function loses the file input names (I usually process the files just by type).

<?php

function incoming_files() {
   
$files $_FILES;
   
$files2 = [];
    foreach (
$files as $input => $infoArr) {
       
$filesByInput = [];
        foreach (
$infoArr as $key => $valueArr) {
            if (
is_array($valueArr)) { // file input "multiple"
               
foreach($valueArr as $i=>$value) {
                   
$filesByInput[$i][$key] = $value;
                }
            }
            else { 
// -> string, normal file input
               
$filesByInput[] = $infoArr;
                break;
            }
        }
       
$files2 array_merge($files2,$filesByInput);
    }
   
$files3 = [];
    foreach(
$files2 as $file) { // let's filter empty & errors
       
if (!$file['error']) $files3[] = $file;
    }
    return 
$files3;
}

$tmpFiles incoming_files();

?>

will transform this: 

Array
(
    [files1] => Array
        (
            [name] => facepalm.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/php3zU3t5
            [error] => 0
            [size] => 31059
        )

    [files2] => Array
        (
            [name] => Array
                (
                    [0] => facepalm2.jpg
                    [1] => facepalm3.jpg
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/phpJutmOS
                    [1] => /tmp/php9bNI8F
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )

            [size] => Array
                (
                    [0] => 78085
                    [1] => 61429
                )

        )

)

into this: 

Array
(
    [0] => Array
        (
            [name] => facepalm.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/php3zU3t5
            [error] => 0
            [size] => 31059
        )

    [1] => Array
        (
            [name] => facepalm2.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/phpJutmOS
            [error] => 0
            [size] => 78085
        )

    [2] => Array
        (
            [name] => facepalm3.jpg
            [type] => image/jpeg
            [tmp_name] => /tmp/php9bNI8F
            [error] => 0
            [size] => 61429
        )

)
2017-09-07 13:36:54
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Автор:
For uploading only one file,  my version, that checks if the file is uploaded.

function processForm($messages) {
        if ($_FILES['file']["error"] == UPLOAD_ERR_OK ) {
    if($_FILES['file']['type'] != "text/csv" && $_FILES['file']['type'] != "application/vnd.ms-excel") {
                $messages->add_error('Wrong format.');
                return false;
    } 
elseif ( !move_uploaded_file( $_FILES['file']["tmp_name"], "files/" . 
basename( $_FILES['file']["name"] ) ) ) {
                $messages->add_error('Upload failed');
                return false;
                } 
        }
       else {
    $messages->add_error("Something went wrong.");
    return false;
       }
    return true;
}

if (isset($_POST['btnUpload'])) {
    if(is_uploaded_file($_FILES['file']['tmp_name'])) {
         if (processForm($messages)) {
                $fileName = "files/" . basename($_FILES['file']['name']);
                parseAndInsert($fileName, $db);
                $messages->add_message("Success");
            }
            }
    else {
        $messages->add_error('File did not upload');
    }
}

function parseAndInsert($fileName, $db) {
        $file = fopen($fileName, "r");
        while($line = fgetcsv($file, 1000, ";")) {
            $db->addGrade(intval($line[0]), intval($line[1]), intval($line[2]));
        }
    fclose($file);
    }
2019-06-09 20:11:36
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Good normalized the $_FILES array so

function getRequestFiles()
 {
        $result = array();

        if (is_array($_FILES)) {
            foreach($_FILES as $name => $fileArray) {
                $row = array();
                foreach ($fileArray as $key => &$data) {
                    findNoArrayElementAndReplace($data, $key);
                    $row = array_merge_recursive($row, $data);
                }
                $result[$name] = $row;
            }
        }

        return $result;
}

function findNoArrayElementAndReplace(&$array, $key)
 {
        if (is_array($array)) {
            foreach ($array as &$item) {
                findNoArrayElementAndReplace($item, $key);
            }
        } else {
            $array = array(
                $key => $array
            );
        }
}
2019-11-27 14:30:41
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Best way to check if $_FILES is empty or not is to check if the name of the index 0 is set. 

<?php 
if ($_FILES['fieldname']['name'][0] != ""){
 
//Code goes here!
}
?>
2021-02-23 19:17:03
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Автор:
this is frustrating that the explanations redirected by anchors are providing unsufficient information or even worst is provide nothing. instead, looking for people to make the resources locale, you MUST provide approprate documentation for everybody.
2021-03-14 11:21:17
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Comprehensive iterator for direct "flat" access for any-level arrayed <input>, in any combination: 

<?php 
foreach( new RecursiveIteratorIterator( new FILES_flattened ) as $key => $data ) {
   
var_dump$key$data );
   
// Here's how to generate an automated name that incorporates the "node path", e.g. "picture[10][5]"
   
echo $key.( empty( $data'pos' ] ) ? '' '['implode']['$data'pos' ] ) . ']' );
}

class 
FILES_flattened implements RecursiveIterator 
{
    protected 
$list_array;
    protected 
$basename;
    protected 
$current;
   
    public function 
__construct()
    {
       
$this->list_array $_FILES;
    }
 
    public function 
rewind(): void
   
{
        if ( 
false === $current reset$this->list_array ) or null === $this->basename key$this->list_array ) ) {
           
$this->current null;
            return;
        }
       
       
$current'pos' ] = array();
       
$this->current $current;
    }
   
    public function 
next(): void
   
{
        if ( 
false === $current next$this->list_array ) or null === $this->basename key$this->list_array ) ) {
           
$this->current null;
            return;
        }
       
       
$current'pos' ] = array();
       
$this->current $current;
    }
   
    public function 
valid(): bool
   
{
        return 
null !== key$this->list_array );        // End of array (inc. empty array).
   
}
   
    public function 
hasChildren(): bool
   
{
        return 
is_array$this->current'name' ] );
    }
   
    public function 
getChildren(): FILES_nested
   
{
        return new 
FILES_nested$this->basename$this->current );
    }
   
    public function 
current()
    {
        return 
$this->current;
    }
   
    public function 
key()
    {
        return 
$this->basename;
    }
}

class 
FILES_nested extends FILES_flattened implements RecursiveIterator
{
    private 
$node_array;
    private 
$position;
   
    public function 
__constructstring $basename, array $node_array )
    {
       
$this->basename $basename;
       
$this->list_array $node_array'name' ];

       
$this->position $node_array'pos' ];
        unset( 
$node_array'pos' ] );
       
       
$this->node_array $node_array;
    }
   
    public function 
rewind(): void
   
{
        if ( 
false === reset$this->list_array ) or null === $key key$this->list_array ) ) {
           
$this->current null;
            return;
        }
       
$this->current $this->normalize$key );
    }
   
    public function 
next(): void
   
{
        if ( 
false === next$this->list_array ) or null === $key key$this->list_array ) ) {
           
$this->current null;
            return;
        }
       
$this->current $this->normalize$key );
    }
   
    private function 
normalize$key ): array 
    {
       
$normalized array_combinearray_keys$this->node_array ), array_column$this->node_array$key) );
       
       
$position $this->position;
       
$position[] = $key;
       
       
$normalized'pos' ] = $position;
        return 
$normalized;
    }
}
2021-06-07 22:50:18
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
The attached "new FILES_flattened" class flattens the $_FILES array, for any combination of "single" files, multi-file arrays with given indices [i] or multi-file array with [] indices, even multi-level multi-file arrays [i][j]...[n].

An additional element "pos" in the flattened array will hold the "node path" for each file of a multi-file array, e.g. 'pos' => [ i, j ], so that it can be used for proper disposition of the file in database array, or for creating automated names that append each index level.

Most importantly, the HTML input "name" property and all other information remains fully intact.

HTML <form> snippet:
 <input type="hidden" name="MAX_FILE_SIZE" value="2500000" title="Permitted bytes per file." />
 <input type="file" name="pictures[4]" accept="image/*"/>
 <input type="file" name="thumb" accept="image/*" />
 <input type="file" name="pictures[]" accept="image/*" />
 <input type="file" name="pictures[10][5]" accept="image/*" />
 <input type="file" name="pictures[17]" accept="image/*" />

Result :
A flat array, one element per <input> field, each will have this structure:

...
'pictures' => array (size=6)
  'name' => 'ClientFilename.jpg' 
  'type' => 'image/jpeg' (
  'tmp_name' => '...\php8D2C.tmp' 
  'error' => 0
  'size' => 1274994
  'pos' => array (size=2)
      0 => 10
      1 => 5
...

with nothing more than a simple foreach:

<?php 
foreach( new RecursiveIteratorIterator( new FILES_flattened ) as $key => $data ) {
   
var_dump$key$data );
   
   
// Here's how to use the "pos" element, to generate an automated name
    // that incorporates the "node path", e.g. "picture[10][5]"
   
echo $key.( empty( $data'pos' ] ) ? '' '['implode']['$data'pos' ] ) . ']' );
}
2021-06-07 22:56:51
http://php5.kiev.ua/manual/ru/reserved.variables.files.html
Error code returned in $_FILES['userfile']['error'].

■UPLOAD_ERROR_OK, value 0, means no error occurred.
 ■ UPLOAD_ERR_INI_SIZE, value 1, means that the size of the uploaded file exceeds the
maximum value specified in your php.ini file with the upload_max_filesize directive.
 ■ UPLOAD_ERR_FORM_SIZE, value 2, means that the size of the uploaded file exceeds the
maximum value specified in the HTML form in the MAX_FILE_SIZE element.
 ■ UPLOAD_ERR_PARTIAL, value 3, means that the file was only partially uploaded.
 ■ UPLOAD_ERR_NO_FILE, value 4, means that no file was uploaded.
 ■ UPLOAD_ERR_NO_TMP_DIR, value 6, means that no temporary directory is specified in the
php.ini.
 ■ UPLOAD_ERR_CANT_WRITE, value 7, means that writing the file to disk failed.
 ■ UPLOAD_ERR_EXTENSION, value 8, means that a PHP extension stopped the file upload
process.
2023-11-08 18:00:57
http://php5.kiev.ua/manual/ru/reserved.variables.files.html

    Поддержать сайт на родительском проекте КГБ