note 124901 added to features.file-upload.post-method

From: Date: Sat, 11 Apr 2020 23:13:21 +0000
Subject: note 124901 added to features.file-upload.post-method
Groups: php.notes 
Request: Send a blank email to php-notes+get-214456@lists.php.net to get a copy of this message
mpyw is right, PSR-7 is awesome but a little overkill for simple projects (in my opinion). Here's an example of function that returns the file upload metadata in a (PSR-7 *like*) normalized tree. This function deals with whatever dimension of upload metadata. I kept the code extremely simple, it doesn't validate anything in $_FILES, etc... AND MOST IMPORTANTLY, it calls array_walk_recursive in an *undefined behaviour* way!!! You can test it against the examples of the PSR-7 spec ( https://www.php-fig.org/psr/psr-7/#16-uploaded-files ) and try to add your own checks that will detect the error in the last example ^^ <?php /** * THIS CODE IS ABSOLUTELY NOT MEANT FOR PRODUCTION !!! MAY ITS INSIGHTS HELP YOU !!! */ function getNormalizedFiles() { $normalized = array(); if ( isset($_FILES) ) { foreach ( $_FILES as $field => $metadata ) { $normalized[$field] = array(); // needs initialization for array_replace_recursive foreach ( $metadata as $meta => $data ) { // $meta is 'tmp_name', 'error', etc... if ( is_array($data) ) { // insert the current meta just before each leaf !!! WRONG USE OF ARRAY_WALK_RECURSIVE !!! array_walk_recursive($data, function (&$v,$k) use ($meta) { $v = array( $meta => $v ); }); // fuse the current metadata with the previous ones $normalized[$field] = array_replace_recursive($normalized[$field], $data); } else { $normalized[$field][$meta] = $data; } } } } return $normalized; } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.195 (proxied: 82.233.112.48) ---- Manual Page -- http://php.net/manual/en/features.file-upload.post-method.php Edit -- https://master.php.net/note/edit/124901 Del: integrated -- https://master.php.net/note/delete/124901/integrated Del: useless -- https://master.php.net/note/delete/124901/useless Del: bad code -- https://master.php.net/note/delete/124901/bad+code Del: spam -- https://master.php.net/note/delete/124901/spam Del: non-english -- https://master.php.net/note/delete/124901/non-english Del: in docs -- https://master.php.net/note/delete/124901/in+docs Del: other reasons-- https://master.php.net/note/delete/124901 Reject -- https://master.php.net/note/reject/124901 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#214456) next »