downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

$_REQUEST> <$_POST
[edit] Last updated: Sat, 07 Jan 2012

view this page in

$_FILES

$HTTP_POST_FILES [deprecated]

(PHP 4 >= 4.1.0, PHP 5)

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

설명

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)

변경점

버전 설명
4.1.0 Introduced $_FILES that deprecated $HTTP_POST_FILES.

주의

Note:

이는 '자동전역' 변수입니다. 스크립트의 모든 영역에서 사용할 수 있습니다. 함수나 메쏘드 안에서 접근하기 위해서 global $variable;를 할 필요가 없습니다.

참고



$_REQUEST> <$_POST
[edit] Last updated: Sat, 07 Jan 2012
 
add a note add a note User Contributed Notes $_FILES
Alexandre Teles 19-May-2012 10:31
You can check error index this way:

<?php

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

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

processFile();

?>
codycoder at me dot com 27-Mar-2012 07:51
I find that using sizeof() works ok.

EG:

if(sizeof($_FILES)!=0){
 handleFiles();
}
yuriy dot nayda at gmail dot com 15-Mar-2012 10:49
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");
BigShark666 at gmail dot com 21-Nov-2011 03:51
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
                )
        )
)
*/
?>
kbolyshev at gmail dot com 17-Nov-2011 09:39
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;
?>
unca dot alby at gmail dot com 02-Sep-2011 07:31
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.
Sbastien 13-Jun-2011 01:36
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.)
Anonymous 26-Apr-2011 08:48
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.
Anonymous 18-Apr-2011 12:23
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....   
       
}
?>
John 03-Feb-2011 11:14
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
mwgamera at gmail dot com 12-Aug-2009 11:40
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.
calurion at gmail dot com 29-Jun-2009 10:51
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
Sam 21-May-2009 01:08
This is REQUIRED by the xhtml specs.
dewi at dewimorgan dot com 18-Mar-2009 04:35
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']
andrewpunch at bigfoot dot com 16-Jan-2009 05:16
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.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites