Students Empire

Learn Something New
Home

Learn PHP


reserved variable get $_FILES

$HTTP_POST_FILES



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


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.


$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)


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.

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


Example


                  <?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
                                )
                        )
                )
                */ 
                ?>
              


Example


                  <?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();
                ?>
              


Example


                  <?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;
                }
                ?>