Skip to content

Instantly share code, notes, and snippets.

@blar
Created May 4, 2013 10:28
Show Gist options
  • Select an option

  • Save blar/5517078 to your computer and use it in GitHub Desktop.

Select an option

Save blar/5517078 to your computer and use it in GitHub Desktop.
<?php
class UploadManager {
public $fields = array();
public function __construct() {
$this->fields = $this->parse($_FILES);
}
protected function parse($fields) {
$result = array();
foreach($this->uglify($fields) as $fieldName => $fieldValues) {
foreach($fieldValues as $name => $values) {
foreach($values as $index => $value) {
$result[$fieldName][$index][$name] = $value;
}
}
}
return $result;
}
protected function uglify($fields) {
foreach($fields as $fieldName => $fieldValues) {
foreach($fieldValues as $name => $value) {
if(!is_array($value)) {
$fields[$fieldName][$name] = array($value);
}
}
}
return $fields;
}
public function getFiles($name) {
if(!array_key_exists($name, $this->fields)) {
return array();
}
return $this->fields[$name];
}
public function getFile($name) {
$files = $this->getFiles($name);
return array_shift($files);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment