note 96365 deleted from features.file-upload.multiple by cmb
| From: | cmb@php.net | Date: | Tue, 25 Aug 2015 16:10:46 +0000 |
| Subject: | note 96365 deleted from features.file-upload.multiple by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-203468@lists.php.net to get a copy of this message | ||
Note Submitter: christiaan at baartse dot nl
----
After trying dozens of ways that are supposed to fix the wonkyness of the $_FILES array I
didn't find any that could work with a input name like: userfile[christiaan][][][is][gaaf][]
So I came up with this class
<?php
/**
* A class that takes the pain out of the $_FILES array
* @author Christiaan Baartse <christiaan@baartse.nl>
*/
class UploadedFiles extends ArrayObject
{
public function current() {
return $this->_normalize(parent::current());
}
public function offsetGet($offset) {
return $this->_normalize(parent::offsetGet($offset));
}
protected function _normalize($entry) {
if(isset($entry['name']) && is_array($entry['name'])) {
$files = array();
foreach($entry['name'] as $k => $name) {
$files[$k] = array(
'name' => $name,
'tmp_name' => $entry['tmp_name'][$k],
'size' => $entry['size'][$k],
'type' => $entry['type'][$k],
'error' => $entry['error'][$k]
);
}
return new self($files);
}
return $entry;
}
}
?>
This allows you to access a file uploaded using the following inputtype
<input type="file" name="userfile[christiaan][][][is][gaaf][]" />
like
<?php
$files = new UploadedFiles($_FILES);
var_dump($files['userfile']['christiaan'][0][0]['is']['gaaf'][0]);
// or
foreach($files['userfile']['christiaan'][0][0]['is']['gaaf']
as $file) {
var_dump($file);
}
?>