[PHP-NOTES] note 130243 added to reserved.variables.files

[email protected] ("[email protected]")
Newsgroups php.notes
Message-ID <[email protected]>
If anyone still needs it: class for sorting an array of files of unknown nesting depth

```

class __FILES
{
    protected $currentKey = null;

    protected $input = array();

    protected $output= array();

    protected $requiredFields = array('name', 'type', 'tmp_name', 'error', 'size', 'full_path');

    public function __construct(array $input)
    {
        $this->input = $input;
    }

    public function setRequiredFields(...$_)
    {
        $this->requiredFields = $_;
        return $this;
    }

    public function prepare()
    {
        $this->prepareMultilevelFilesArray($this->input, $this->output);
        return $this;
    }

    public function output()
    {
        return $this->output;
    }

    protected function prepareMultilevelFilesArray($input, &$output)
    {
        if(is_array($input)){
            foreach($input as $key => $values){
                // lock field names `error`, `size`, `name`, `type`, `tmp_name`
                if($this->checkRequiredFields($input)){
                    $this->currentKey = $key;
                    $this->prepareMultilevelFilesArray($values, $output);
                }else
                    if(is_array($values)){
                        $this->prepareMultilevelFilesArray($values, $output[$key]);
                    }else{
                        $output[$key][$this->currentKey] = $values;
                    }
            }
        }else{
            $output[$this->currentKey] = $input;
        }
    }

    protected function checkRequiredFields($input)
    {
        foreach($this->requiredFields as $field){
            if(!isset($input[$field])){ return false;}
        }
        return true;
    }
}

```

Call:

```

print '<form enctype="multipart/form-data" method="POST">';
print '<input type="file" name="s[t][t1][t2][t3][t4][t5][]" multiple="multiple" />';
print '<input type="file" name="s[t][t1][t2][t3][t4][t5]" multiple="multiple" />';
print '<input type="file" name="s[t][t1][t2][][t3][t4][t5]" multiple="multiple" />';
print '<input type="file" name="s[t][t1][t2][][t3][][t4][t5]" multiple="multiple" />';
print '<input type="file" name="s[t][t1][t2][][t3][][t4][t5][]" multiple="multiple" />';
print '<input type="submit"/>';
print '</form>';

$input = $_FILES;
$files = new __FILES($input);
$output = $files->setRequiredFields('name', 'type', 'tmp_name', 'error', 'size'/*, 'full_path'*/)	// key `full_path` not available on PHP-server (`php -S`), and available on apache2-server
	->prepare()->output();

print "<pre>";
print_r([$input, $output]);exit;

```
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 95.25.128.43)
----
Manual Page -- https://php.net/manual/en/reserved.variables.files.php
Edit        -- https://main.php.net/note/edit/130243
Del: integrated  -- https://main.php.net/note/delete/130243/integrated
Del: useless     -- https://main.php.net/note/delete/130243/useless
Del: bad code    -- https://main.php.net/note/delete/130243/bad+code
Del: spam        -- https://main.php.net/note/delete/130243/spam
Del: non-english -- https://main.php.net/note/delete/130243/non-english
Del: in docs     -- https://main.php.net/note/delete/130243/in+docs
Del: other reasons-- https://main.php.net/note/delete/130243
Reject      -- https://main.php.net/note/reject/130243
Search      -- https://main.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.