Created
May 4, 2013 10:45
-
-
Save blar/5517111 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class UploadManager { | |
| public $fields = array(); | |
| public function __construct($files = NULL) { | |
| if(is_null($files)) { | |
| $files = $_FILES; | |
| } | |
| $this->fields = $files; | |
| } | |
| protected function parseField($fieldName) { | |
| if(!array_key_exists($fieldName, $this->fields)) { | |
| return array(); | |
| } | |
| $result = array(); | |
| foreach($this->uglify($this->fields[$fieldName]) as $name => $values) { | |
| foreach($values as $index => $value) { | |
| $result[$index][$name] = $value; | |
| } | |
| } | |
| return $result; | |
| } | |
| protected function uglify($fields) { | |
| foreach($fields as $name => $value) { | |
| if(!is_array($value)) { | |
| $fields[$name] = array($value); | |
| } | |
| } | |
| return $fields; | |
| } | |
| public function getFiles($name) { | |
| return $this->parseField($name); | |
| } | |
| public function getFile($name) { | |
| $files = $this->getFiles($name); | |
| return array_shift($files); | |
| } | |
| } | |
| $files = array( | |
| 'foo' => array( | |
| 'name' => 'foo', | |
| 'size' => 23 | |
| ), | |
| 'bar' => array( | |
| 'name' => 'bar', | |
| 'size' => 42 | |
| ) | |
| ); | |
| $uploadManager = new UploadManager($files); | |
| var_dump($uploadManager->getFile('foo')); | |
| var_dump($uploadManager->getFiles('foo')); | |
| $files = array( | |
| 'foobar' => array( | |
| 'name' => array('foo', 'bar'), | |
| 'size' => array(23, 42) | |
| ) | |
| ); | |
| $uploadManager = new UploadManager($files); | |
| var_dump($uploadManager->getFile('foobar')); | |
| var_dump($uploadManager->getFiles('foobar')); | |
| array(2) { | |
| 'name' => | |
| string(3) "foo" | |
| 'size' => | |
| int(23) | |
| } | |
| array(1) { | |
| [0] => | |
| array(2) { | |
| 'name' => | |
| string(3) "foo" | |
| 'size' => | |
| int(23) | |
| } | |
| } | |
| array(2) { | |
| 'name' => | |
| string(3) "foo" | |
| 'size' => | |
| int(23) | |
| } | |
| array(2) { | |
| [0] => | |
| array(2) { | |
| 'name' => | |
| string(3) "foo" | |
| 'size' => | |
| int(23) | |
| } | |
| [1] => | |
| array(2) { | |
| 'name' => | |
| string(3) "bar" | |
| 'size' => | |
| int(42) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment