Advertisement
Crushed

Untitled

Feb 1st, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php
  2.  
  3. require_once __DIR__ . '/protected/config/config.php';
  4.  
  5. $allowedTypes = array('image/png', 'image/jpeg', 'image/gif', 'video/webm');
  6.  
  7. if ( ! isset($_POST['password']) || $_POST['password'] !== PASSKEY) {
  8.     die('error,e-401');
  9. }
  10.  
  11. if ( ! (filesize($_FILES['image']['tmp_name']) > 0 && in_array($_FILES['image']['type'], $allowedTypes))) {
  12.     die('error,e-415');
  13. }
  14.  
  15. if ($_FILES['image']['error'] > 0) {
  16.     die('error,e-500');
  17. }
  18.  
  19. $dir = __DIR__ . '/images/';
  20.  
  21. saveImage($_FILES['image']['type'], $_FILES['image']['tmp_name']);
  22.  
  23. function generateNewHash($type)
  24. {
  25.     $an = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  26.     $str = '';
  27.  
  28.     for ($i = 0; $i < 5; $i++) {
  29.         $str .= substr($an, rand(0, strlen($an) - 1), 1);
  30.     }
  31.  
  32.     if ( ! file_exists(__DIR__ . "/images/$type/$str.$type")) {
  33.         return $str;
  34.     } else {
  35.         return generateNewHash($type);
  36.     }
  37. }
  38.  
  39. function saveImage($mimeType, $tempName)
  40. {
  41.     global $dir;
  42.  
  43.     $mimeTypeArray = explode('/', $mimeType);
  44.     $type = $mimeTypeArray[1];
  45.     $hash = generateNewHash($type);
  46.  
  47.     if (move_uploaded_file($tempName, $dir . "$type/$hash.$type")) {
  48.         die('success,' . (RAW_IMAGE_LINK ? "images/$type/$hash.$type" : ($type == 'png' ? '' : substr($type, 0, 1) . '/') . "$hash" . (IMAGE_EXTENSION ? ".$type" : '')));
  49.     }
  50.  
  51.     die('error,e-500x');
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement