| 703 | | static public function getFiles($key = null) |
|---|
| 704 | | { |
|---|
| 705 | | return is_null($key) ? $_FILES : (isset($_FILES[$key]) ? $_FILES[$key] : array()); |
|---|
| | 704 | public function getFiles($key = null) |
|---|
| | 705 | { |
|---|
| | 706 | if (false === $this->fileArrayFixed) |
|---|
| | 707 | { |
|---|
| | 708 | $files = self::convertFileInformation($_FILES); |
|---|
| | 709 | $this->fileArrayFixed = true; |
|---|
| | 710 | } |
|---|
| | 711 | |
|---|
| | 712 | return is_null($key) ? $files : (isset($files[$key]) ? $files[$key] : array()); |
|---|
| | 713 | } |
|---|
| | 714 | |
|---|
| | 715 | /** |
|---|
| | 716 | * Converts uploaded file array to a format following the $_GET and $POST naming convention. |
|---|
| | 717 | * |
|---|
| | 718 | * It's safe to pass an already converted array, in which case this method just returns the original array unmodified. |
|---|
| | 719 | * |
|---|
| | 720 | * @param array $taintedFiles An array representing uploaded file information |
|---|
| | 721 | * |
|---|
| | 722 | * @return array An array of re-ordered uploaded file information |
|---|
| | 723 | */ |
|---|
| | 724 | static public function convertFileInformation(array $taintedFiles) |
|---|
| | 725 | { |
|---|
| | 726 | $files = array(); |
|---|
| | 727 | foreach ($taintedFiles as $key => $data) |
|---|
| | 728 | { |
|---|
| | 729 | $files[$key] = self::fixPhpFilesArray($data); |
|---|
| | 730 | } |
|---|
| | 731 | |
|---|
| | 732 | return $files; |
|---|
| | 733 | } |
|---|
| | 734 | |
|---|
| | 735 | static protected function fixPhpFilesArray($data) |
|---|
| | 736 | { |
|---|
| | 737 | $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type'); |
|---|
| | 738 | $keys = array_keys($data); |
|---|
| | 739 | sort($keys); |
|---|
| | 740 | |
|---|
| | 741 | if ($fileKeys != $keys || !isset($data['name']) || !is_array($data['name'])) |
|---|
| | 742 | { |
|---|
| | 743 | return $data; |
|---|
| | 744 | } |
|---|
| | 745 | |
|---|
| | 746 | $files = $data; |
|---|
| | 747 | foreach ($fileKeys as $k) |
|---|
| | 748 | { |
|---|
| | 749 | unset($files[$k]); |
|---|
| | 750 | } |
|---|
| | 751 | foreach (array_keys($data['name']) as $key) |
|---|
| | 752 | { |
|---|
| | 753 | $files[$key] = self::fixPhpFilesArray(array( |
|---|
| | 754 | 'error' => $data['error'][$key], |
|---|
| | 755 | 'name' => $data['name'][$key], |
|---|
| | 756 | 'type' => $data['type'][$key], |
|---|
| | 757 | 'tmp_name' => $data['tmp_name'][$key], |
|---|
| | 758 | 'size' => $data['size'][$key], |
|---|
| | 759 | )); |
|---|
| | 760 | } |
|---|
| | 761 | |
|---|
| | 762 | return $files; |
|---|