Skip to content

Instantly share code, notes, and snippets.

@noorwachid
Last active December 29, 2021 02:09
Show Gist options
  • Save noorwachid/fce70a3a2d96502c2805248c46fc23f9 to your computer and use it in GitHub Desktop.
Save noorwachid/fce70a3a2d96502c2805248c46fc23f9 to your computer and use it in GitHub Desktop.
Sanner $_FILES PHP Structure

The $_FILES has very I'd say unique structure, I know the intend is to maintain the multi-dimention keys, the behaviour is predictable but it's hard to iterate.

The single purpose of this class is to move the value's keys to the leaf node. You can even modify to make the leaf node as an object with move method to even ease up the upload mechanism.

// original
print_r($_FILES);

// restructured
print_r(UploadedFile::build());
// original
Array
(
    [one] => Array
        (
            [name] => ss_break.png
            [type] => image/png
            [tmp_name] => /tmp/phppY8lSV
            [error] => 0
            [size] => 43582
        )

    [multiple] => Array
        (
            [name] => Array
                (
                    [0] => LeleBreeder.png
                )

            [type] => Array
                (
                    [0] => image/png
                )

            [tmp_name] => Array
                (
                    [0] => /tmp/php6GSj1X
                )

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

            [size] => Array
                (
                    [0] => 14284
                )

        )

    [crazy] => Array
        (
            [name] => Array
                (
                    [nested] => Array
                        (
                            [keys] => PlainUbuntu.png
                        )

                )

            [type] => Array
                (
                    [nested] => Array
                        (
                            [keys] => image/png
                        )

                )

            [tmp_name] => Array
                (
                    [nested] => Array
                        (
                            [keys] => /tmp/php4boHkj
                        )

                )

            [error] => Array
                (
                    [nested] => Array
                        (
                            [keys] => 0
                        )

                )

            [size] => Array
                (
                    [nested] => Array
                        (
                            [keys] => 25668
                        )

                )

        )

)

// restructured
Array
(
    [one] => Array
        (
            [originalPath] => ss_break.png
            [path] => /tmp/phppY8lSV
            [contentType] => image/png
            [size] => 43582
            [errorCode] => 0
        )

    [multiple] => Array
        (
            [0] => Array
                (
                    [originalPath] => LeleBreeder.png
                    [path] => /tmp/php6GSj1X
                    [contentType] => image/png
                    [size] => 14284
                    [errorCode] => 0
                )

        )

    [crazy] => Array
        (
            [nested] => Array
                (
                    [keys] => Array
                        (
                            [originalPath] => PlainUbuntu.png
                            [path] => /tmp/php4boHkj
                            [contentType] => image/png
                            [size] => 25668
                            [errorCode] => 0
                        )

                )

        )

)
<?php
class UploadedFile
{
public static function walk(&$originalPath, &$path, &$contentType, &$size, &$errorCode)
{
if (is_array($originalPath)) {
foreach ($originalPath as $key => &$value)
self::walk($value, $path[$key], $contentType[$key], $size[$key], $errorCode[$key]);
} else {
$originalPath = [
'originalPath' => $originalPath,
'path' => $path,
'contentType' => $contentType,
'size' => $size,
'errorCode' => $errorCode,
];
}
}
public static function build()
{
// swap second keys
$rootNode = [];
foreach ($_FILES as $key => $value) {
foreach ($value as $key2 => $value2) {
$rootNode[$key2][$key] = $value2;
}
}
// swap first and last keys
self::walk($rootNode['name'], $rootNode['tmp_name'], $rootNode['type'], $rootNode['size'], $rootNode['error']);
// remove unused keys
unset($rootNode['tmp_name']);
unset($rootNode['type']);
unset($rootNode['size']);
unset($rootNode['error']);
return $rootNode['name'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment