Guest User

UploadClass

a guest
Jun 5th, 2022
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.62 KB | None | 0 0
  1. <?php
  2. namespace foundationphp;
  3.  
  4. class UploadFile
  5. {
  6.     protected $destination;
  7.     protected $messages = array();
  8.     protected $maxSize = 51200;
  9.     protected $permittedTypes = array(
  10.             'image/jpeg',
  11.             'image/pjpeg',
  12.             'image/gif',
  13.             'image/png',
  14.             'image/webp'
  15.     );
  16.     protected $newName;
  17.     protected $typeCheckingOn = true;
  18.     protected $notTrusted = array('bin', 'cgi', 'exe', 'js', 'pl', 'php', 'py', 'sh');
  19.     protected $suffix = '.upload';
  20.     protected $renameDuplicates;
  21.    
  22.     public function __construct($uploadFolder)
  23.     {
  24.         if (!is_dir($uploadFolder) || !is_writable($uploadFolder)) {
  25.             throw new \Exception("$uploadFolder must be a valid, writable folder.");
  26.         }
  27.         if ($uploadFolder[strlen($uploadFolder)-1] != '/') {
  28.             $uploadFolder .= '/';
  29.         }
  30.         $this->destination = $uploadFolder;
  31.     }
  32.    
  33.     public function setMaxSize($bytes)
  34.     {
  35.         $serverMax = self::convertToBytes(ini_get('upload_max_filesize'));
  36.         if ($bytes > $serverMax) {
  37.             throw new \Exception('Maximum size cannot exceed server limit for individual files: ' .
  38.     self::convertFromBytes($serverMax));
  39.         }
  40.         if (is_numeric($bytes) && $bytes > 0) {
  41.             $this->maxSize = $bytes;
  42.         }
  43.     }
  44.    
  45.     public static function convertToBytes($val)
  46.     {
  47.         $val = trim($val);
  48.         $last = strtolower($val[strlen($val)-1]);
  49.         if (in_array($last, array('g', 'm', 'k'))){
  50.             switch ($last) {
  51.                 case 'g':
  52.                     $val *= 1024;
  53.                 case 'm':
  54.                     $val *= 1024;
  55.                 case 'k':
  56.                     $val *= 1024;
  57.             }
  58.         }
  59.         return $val;
  60.     }
  61.    
  62.     public static function convertFromBytes($bytes)
  63.     {
  64.         $bytes /= 1024;
  65.         if ($bytes > 1024) {
  66.             return number_format($bytes/1024, 1) . ' MB';
  67.         } else {
  68.             return number_format($bytes, 1) . ' KB';
  69.         }
  70.     }
  71.    
  72.     public function allowAllTypes($suffix = null)
  73.     {
  74.         $this->typeCheckingOn = false;
  75.         if (!is_null($suffix)) {
  76.             if (strpos($suffix, '.') === 0 || $suffix == '') {
  77.                 $this->suffix = $suffix;
  78.             } else {
  79.                 $this->suffix = ".$suffix";
  80.             }
  81.         }
  82.     }
  83.    
  84.     public function upload($renameDuplicates = true)
  85.     {
  86.         $this->renameDuplicates = $renameDuplicates;
  87.         $uploaded = current($_FILES);
  88.         if (is_array($uploaded['name'])) {
  89.             foreach ($uploaded['name'] as $key => $value) {
  90.                 $currentFile['name'] = $uploaded['name'][$key];
  91.                 $currentFile['type'] = $uploaded['type'][$key];
  92.                 $currentFile['tmp_name'] = $uploaded['tmp_name'][$key];
  93.                 $currentFile['error'] = $uploaded['error'][$key];
  94.                 $currentFile['size'] = $uploaded['size'][$key];
  95.                 if ($this->checkFile($currentFile)) {
  96.                     $this->moveFile($currentFile);
  97.                 }
  98.             }
  99.         } else {
  100.             if ($this->checkFile($uploaded)) {
  101.                 $this->moveFile($uploaded);
  102.             }
  103.         }
  104.     }
  105.    
  106.     public function getMessages()
  107.     {
  108.         return $this->messages;
  109.     }
  110.    
  111.     protected function checkFile($file)
  112.     {
  113.         if ($file['error'] != 0) {
  114.             $this->getErrorMessage($file);
  115.             return false;
  116.         }
  117.         if (!$this->checkSize($file)) {
  118.             return false;
  119.         }
  120.         if ($this->typeCheckingOn) {
  121.             if (!$this->checkType($file)) {
  122.                 return false;
  123.             }
  124.         }
  125.         $this->checkName($file);
  126.         return true;
  127.     }
  128.    
  129.     protected function getErrorMessage($file)
  130.     {
  131.         switch($file['error']) {
  132.             case 1:
  133.             case 2:
  134.                 $this->messages[] = $file['name'] . ' is too big: (max: ' .
  135.                 self::convertFromBytes($this->maxSize) . ').';
  136.                 break;
  137.             case 3:
  138.                 $this->messages[] = $file['name'] . ' was only partially uploaded.';
  139.                 break;
  140.             case 4:
  141.                 $this->messages[] = 'No file submitted.';
  142.                 break;
  143.             default:
  144.                 $this->messages[] = 'Sorry, there was a problem uploading ' . $file['name'];
  145.                 break;
  146.         }
  147.     }
  148.    
  149.     protected function checkSize($file)
  150.     {
  151.         if ($file['size'] == 0) {
  152.             $this->messages[] = $file['name'] . ' is empty.';
  153.             return false;
  154.         } elseif ($file['size'] > $this->maxSize) {
  155.             $this->messages[] = $file['name'] . ' exceeds the maximum size for a file ('
  156.                     . self::convertFromBytes($this->maxSize) . ').';
  157.             return false;
  158.         } else {
  159.             return true;
  160.         }
  161.     }
  162.    
  163.     protected function checkType($file)
  164.     {
  165.         if (in_array($file['type'], $this->permittedTypes)) {
  166.             return true;
  167.         } else {
  168.             $this->messages[] = $file['name'] . ' is not permitted type of file.';
  169.             return false;
  170.         }
  171.     }
  172.    
  173.     protected function checkName($file)
  174.     {
  175.         $this->newName = null;
  176.         $nospaces = str_replace(' ', '_', $file['name']);
  177.         if ($nospaces != $file['name']) {
  178.             $this->newName = $nospaces;
  179.         }
  180.         $nameparts = pathinfo($nospaces);
  181.         $extension = isset($nameparts['extension']) ? $nameparts['extension'] : '';
  182.         if (!$this->typeCheckingOn && !empty($this->suffix)) {
  183.             if (in_array($extension, $this->notTrusted) || empty($extension)) {
  184.                 $this->newName = $nospaces . $this->suffix;
  185.             }
  186.         }
  187.         if ($this->renameDuplicates) {
  188.             $name = isset($this->newName) ? $this->newName : $file['name'];
  189.             $existing = scandir($this->destination);
  190.             if (in_array($name, $existing)) {
  191.                 $i = 1;
  192.                 do {
  193.                     $this->newName = $nameparts['filename'] . '_' . $i++;
  194.                     if (!empty($extension)) {
  195.                         $this->newName .= ".$extension";
  196.                     }
  197.                     if (in_array($extension, $this->notTrusted)) {
  198.                         $this->newName .= $this->suffix;
  199.                     }
  200.                 } while (in_array($this->newName, $existing));
  201.             }
  202.         }
  203.     }
  204.    
  205.     protected function moveFile($file)
  206.     {
  207.         $filename = isset($this->newName) ? $this->newName : $file['name'];
  208.         $success = move_uploaded_file($file['tmp_name'], $this->destination . $filename);
  209.         if ($success) {
  210.             $result = $file['name'] . ' was uploaded successfully';
  211.             if (!is_null($this->newName)) {
  212.                 $result .= ', and was renamed ' . $this->newName;
  213.             }
  214.             $result .= '.';
  215.             $this->messages[] = $result;
  216.         } else {
  217.             $this->messages[] = 'Could not upload ' . $file['name'];
  218.         }
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment