Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class FilesDeShitificator {
  4.     /**
  5.      * Restructures a control with multiple files from $_FILES to a more sane layout
  6.      *
  7.      * @param array $input
  8.      * @return array
  9.      */
  10.     function restructureMultiFileDataArray($input) {
  11.         $output = array();
  12.  
  13.         for ($i = 0; isset($input['tmp_name'][$i]); $i++) {
  14.             $output[$i] = array(
  15.                 'error' => $input['error'][$i],
  16.                 'name' => $input['name'][$i],
  17.                 'type' => $input['type'][$i],
  18.                 'size' => $input['size'][$i],
  19.                 'tmp_name' => $input['tmp_name'][$i],
  20.             );
  21.         }
  22.  
  23.         return $output;
  24.     }
  25.  
  26.     /**
  27.      * Processes data from $_FILES and creates a saner array format
  28.      *
  29.      * @param array $input
  30.      * @return array
  31.      */
  32.     function processFilesSuperglobal($input) {
  33.         $output = array();
  34.  
  35.         foreach ($input as $controlName => $fileData) {
  36.             if (!isset($fileData['tmp_name'], $fileData['error'])) {
  37.                 // input data not a valid $_FILES format array
  38.                 return false;
  39.             }
  40.  
  41.             $output[$controlName] = is_array($fileData['tmp_name']) ? $this->restructureMultiFileDataArray($fileData) : array($fileData);
  42.         }
  43.  
  44.         return $output;
  45.     }
  46. }
  47.  
  48. $processor = &new FilesDeShitificator;
  49. $processedFiles = $processor->processFilesSuperglobal($_FILES);
  50.  
  51. /*
  52. example usage of $processedFiles
  53.  
  54. foreach ($processedFiles as $controlName => $controlFiles) {
  55.     foreach ($controlFiles as $file) {
  56.         // process each file individually here
  57.         // $file is now an array of the following format (taken from one of my test uploads):
  58.         array(5) (
  59.             ["name"]     => string(13) "xinput1_3.dll"
  60.             ["type"]     => string(24) "application/x-msdownload"
  61.             ["tmp_name"] => string(14) "/tmp/phpQvO1FC"
  62.             ["error"]    => int(0)
  63.             ["size"]     => int(81768)
  64.         )
  65.     }
  66. }
  67.  
  68. */
  69. ?>
  70. <html>
  71. <head>
  72.   <title>Upload Test</title>
  73. </head>
  74. <body>
  75. <?php if (isset($_GET['dump'])) { ?>
  76.   <pre>
  77. <?php var_dump($processedFiles); ?>
  78.   </pre>
  79. <?php } ?>
  80.   <form method="post" action="?dump=1" enctype="multipart/form-data">
  81.     <input type="file" name="multiple_files[]" multiple><br>
  82.     <input type="file" name="single_file"><br>
  83.     <input type="submit" value="Go">
  84.   </form>
  85. </body>
  86. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement