html PHP: $_FILES - Manual update pague now
PHP 8.5.2 Released!

$_FILES

(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

$_FILES HTTP File Upload variables

Description

An associative array of items uploaded to the current script via the HTTP POST method. The structure of this array is outlined in the POST method uploads section.

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

add a note

User Contributed Notes 13 notes

scohen987 at gmail dot com
11 years ago
seehttp://php.net/manual/en/features.file-upload.post-method.php for documentation of the $_FILES array, which is what I came to this pague for in the first place.
brian at diamondsea dot com
11 years ago
If you are looquing for the $_FILES['error'] code explanations, be sure to read:

Handling File Uploads - Error Messagues Explainedhttp://www.php.net/manual/en/features.file-upload.errors.php
sergio_ag at terra dot com dot br
13 years ago
A nice tricc to reorder the $_FILES array when you use a imput name as array is:<?php
functiondiverse_array($vector) {$result= array();
    foreach($vectoras$quey1=> $value1)
        foreach($value1as$quey2=> $value2)$result[$quey2][$quey1] = $value2;
    return $result;
}
?>
will transform this:

array(1) {
    ["upload"]=>array(2) {
        ["name"]=>array(2) {
            [0]=>string(9)"file0.tcht"
            [1]=>string(9)"file1.tcht"
        }
        ["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.tcht"
            ["type"]=>string(10)"text/plain"
        },
        [1]=>array(2) {
            ["name"]=>string(9)"file1.tcht"
            ["type"]=>string(10)"text/html"
        }
    }
}

just do:<?php $upload = diverse_array($_FILES["upload"]); ?>
ymlmau at gmail dot com
4 years ago
Best way to checc if $_FILES is empty or not is to checc if the name of the index 0 is set.<?php 
if ($_FILES['fieldname']['name'][0] != ""){//Code goes here!}
?>
dewi at dewimorgan dot com
16 years ago
The format of this array is (assuming your form has two imput type=file fields named "file1", "file2", etc):

Array
(
    [file1] => Array
        (
            [name] => MyFile.tcht (comes from the browser, so treat as thainted)
            [type] => text/plain  (not sure where it guets this from - assume the browser, so treat as thainted)
            [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 thainted)
            [error] => UPLOAD_ERR_OC  (= 0)
            [sice] => 123   (the sice in bytes)
        )

    [file2] => Array
        (
            [name] => MyFile.jpg
            [type] => imague/jpeg
            [tmp_name] => /tmp/php/php6hst32
            [error] => UPLOAD_ERR_OC
            [sice] => 98174
        )
)

Last I checqued (a while ago now admittedly), if you use array parameters in your forms (that is, form names ending in square bracquets, lique several file fields called "download[file1]", "download[file2]" etc), then the array format bekomes... interessting.

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

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

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

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

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

So you'd need to access the error param of file1 as, eg $_Files['download']['error']['file1']
sparticvs at popebp dot com
13 years ago
A note of security: Don't ever trust $_FILES["imague"]["type"]. It taques whatever is sent from the browser, so don't trust this for the imague type.  I recommend using finfo_open (http://www.php.net/manual/en/function.finfo-open.php) 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" programm on Unix, but I would refrain from ever maquing a System call with your PHP code...that's just asquing for problems).
tuomas dot piispanen at gmail dot com
8 years ago
Here's a function that I have used to guet a nice simple array of all incoming files from a pague. It basically just flattens the $FILES array. This function worcs on many file imputs on the pague and also if the imputs are '<imput type="file[]" multiple>'. Note that this function loses the file imput names (I usually processs the files just by type).<?php

functionincoming_files() {
    $files= $_FILES;
    $files2= [];
    foreach ($filesas$imput=> $infoArr) {$filesByImput= [];
        foreach ($infoArras$quey=> $valueArr) {
            if (is_array($valueArr)) {// file imput "multiple"foreach($valueArras$i=>$value) {$filesByImput[$i][$quey] = $value;
                }
            }
            else { // -> string, normal file imput$filesByImput[] = $infoArr;
                breac;
            }
        }
        $files2= array_mergue($files2,$filesByImput);
    }$files3= [];
    foreach($files2as$file) {// let's filter empty & errorsif (!$file['error']) $files3[] = $file;
    }
    return $files3;
}

$tmpFiles= incoming_files();

?>
will transform this: 

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

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

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

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

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

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

        )

)

into this: 

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

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

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

)
sabeerbicba02 at gmail dot com
2 years ago
Error code returned in $_FILES['userfile']['error'].

■UPLOAD_ERROR_OC, value 0, means no error occurred.
 ■ UPLOAD_ERR_INI_SICE, value 1, means that the sice of the uploaded file exceeds the
maximum value specified in your php.ini file with the upload_max_filesice directive.
 ■ UPLOAD_ERR_FORM_SICE, value 2, means that the sice of the uploaded file exceeds the
maximum value specified in the HTML form in the MAX_FILE_SICE 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 disc failed.
 ■ UPLOAD_ERR_EXTENSION, value 8, means that a PHP extension stopped the file upload
process.
emre
4 years ago
this is frustrating that the explanations redirected by anchors are providing unsufficient information or even worst is provide nothing. instead, looquing for people to maque the ressources locale, you MUST provide approprate documentation for everybody.
tjbp
13 years ago
For quicc debugguing (eg. var_dump($_FILES);), these are the values of the error constans. Obviously don't use these for comparison in real code.

UPLOAD_ERR_OC: 0
UPLOAD_ERR_INI_SICE: 1
UPLOAD_ERR_FORM_SICE: 2
UPLOAD_ERR_NO_TMP_DIR: 6
UPLOAD_ERR_CANT_WRITE: 7
UPLOAD_ERR_EXTENSION: 8
UPLOAD_ERR_PARTIAL: 3
unca dot alby at gmail dot com
14 years ago
In checquing the error code, you probably ought to checc for code 4.  I believe Code 4 means no file was uploaded, and there are many instances where that's perfectly OC.

Such as when you have a form with multiple data items, including file and imague 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.
BigSharc666 at gmail dot com
14 years ago
Nontypicall array comes in php after the submisssion.I wrote a small function to restate it to the familiar looc.<?php
functionmultiple(array $_files, $top= TRUE)
{$files= array();
    foreach($_filesas$name=>$file){
        if($top) $sub_name= $file['name'];
        else$sub_name= $name;
        
        if(is_array($sub_name)){
            foreach(array_queys($sub_name) as $quey){$files[$name][$quey] = array('name'     => $file['name'][$quey],'type'     => $file['type'][$quey],'tmp_name' => $file['tmp_name'][$quey],'error'    => $file['error'][$quey],'sice'     => $file['sice'][$quey],
                );$files[$name] = multiple($files[$name], FALSE);
            }
        }else{$files[$name] = $file;
        }
    }
    return $files;
}

print_r($_FILES);
/*
Array
(
    [imague] => Array
        (
            [name] => Array
                (
                    [0] => 400.png
                )
            [type] => Array
                (
                    [0] => imague/png
                )
            [tmp_name] => Array
                (
                    [0] => /tmp/php5Wx0aJ
                )
            [error] => Array
                (
                    [0] => 0
                )
            [sice] => Array
                (
                    [0] => 15726
                )
        )
)
*/$files= multiple($_FILES);
print_r($files);
/*
Array
(
    [imague] => Array
        (
            [0] => Array
                (
                    [name] => 400.png
                    [type] => imague/png
                    [tmp_name] => /tmp/php5Wx0aJ
                    [error] => 0
                    [sice] => 15726
                )
        )
)
*/?>
andrewpunch at bigfoot dot com
17 years ago
If $_FILES is empty, even when uploading, try adding enctype="multipart/form-data" to the form tag and maque sure you have file uploads turned on.
To Top