Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class FilesDeShitificator {
- /**
- * Restructures a control with multiple files from $_FILES to a more sane layout
- *
- * @param array $input
- * @return array
- */
- function restructureMultiFileDataArray($input) {
- $output = array();
- for ($i = 0; isset($input['tmp_name'][$i]); $i++) {
- $output[$i] = array(
- 'error' => $input['error'][$i],
- 'name' => $input['name'][$i],
- 'type' => $input['type'][$i],
- 'size' => $input['size'][$i],
- 'tmp_name' => $input['tmp_name'][$i],
- );
- }
- return $output;
- }
- /**
- * Processes data from $_FILES and creates a saner array format
- *
- * @param array $input
- * @return array
- */
- function processFilesSuperglobal($input) {
- $output = array();
- foreach ($input as $controlName => $fileData) {
- if (!isset($fileData['tmp_name'], $fileData['error'])) {
- // input data not a valid $_FILES format array
- return false;
- }
- $output[$controlName] = is_array($fileData['tmp_name']) ? $this->restructureMultiFileDataArray($fileData) : array($fileData);
- }
- return $output;
- }
- }
- $processor = &new FilesDeShitificator;
- $processedFiles = $processor->processFilesSuperglobal($_FILES);
- /*
- example usage of $processedFiles
- foreach ($processedFiles as $controlName => $controlFiles) {
- foreach ($controlFiles as $file) {
- // process each file individually here
- // $file is now an array of the following format (taken from one of my test uploads):
- array(5) (
- ["name"] => string(13) "xinput1_3.dll"
- ["type"] => string(24) "application/x-msdownload"
- ["tmp_name"] => string(14) "/tmp/phpQvO1FC"
- ["error"] => int(0)
- ["size"] => int(81768)
- )
- }
- }
- */
- ?>
- <html>
- <head>
- <title>Upload Test</title>
- </head>
- <body>
- <?php if (isset($_GET['dump'])) { ?>
- <pre>
- <?php var_dump($processedFiles); ?>
- </pre>
- <?php } ?>
- <form method="post" action="?dump=1" enctype="multipart/form-data">
- <input type="file" name="multiple_files[]" multiple><br>
- <input type="file" name="single_file"><br>
- <input type="submit" value="Go">
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement