reenadak

File Upload

Feb 20th, 2018
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.54 KB | None | 0 0
  1.    
  2.     /*
  3.     - File Upload
  4.     - Allows the upload of a file with specified (or no) limitations.
  5.     - Returns 0 on success
  6.     - Returns 1 if overwrite is disabled and file already exists
  7.     - Returns 2 if it does not meet any valid extensions
  8.     - Returns 3 if a max file size is set and this file exceeds it
  9.     - Returns 4 if width is smaller than min width or height is smaller than min height
  10.     - Returns 5 if width is larger than max width or height is larger than max height
  11.     - Returns 6 if an unexpected error occurred and the file couldn't be uploaded
  12.    
  13.     $file            - $_FILES['filename']; from a form with input type="file" and name="filename"
  14.     $newLocation     - The directory relative to the location of this script to move the file to,
  15.                        directory will be created if it does not exist already.
  16.     $newName         - The new name of the file without the extension (eg. "image")
  17.     $validExtensions - Array containing a list of valid extensions, file will be blocked if it does
  18.                        not have any one of these extensions (set it to null for no limits on extensions)
  19.     $overwrite       - Overwrite the file if it already exists in this place and as this name?
  20.     $isImage         - If only images can be uploaded, set this to true.
  21.     $minImgWidth     - The minimum width the image can be. Set to 0 for no minimum width.
  22.     $minImgHeight    - The minimuim height the image can be. Set to 0 for no minimum height.
  23.     $maxImgWidth     - The maximum width the image can be. Set to 0 for no maximum width.
  24.     $maxImgHeight    - The maximum height the image can be. Set to 0 for no maximum height.
  25.     $maxSize         - Max size the file can be in bytes, set to 0 for unlimited.
  26.     */
  27.     public function upload($file, $newLocation, $newName, $validExtensions = null, $overwrite = true, $isImage = false, $minImgWidth = 0, $minImgHeight = 0, $maxImgWidth = 0, $maxImgHeight = 0, $maxSize = 8388608) {
  28.        
  29.         $fileType = pathinfo($file['name'], PATHINFO_EXTENSION);
  30.         $targFile = $newLocation . $newName . "." . $fileType;
  31.        
  32.         $failed = false;
  33.        
  34.         if (!$overwrite && file_exists($targFile)) {
  35.             return 1;
  36.             $failed = true;
  37.         }
  38.        
  39.         if (!$failed) {
  40.             if ($validExtensions != null) {
  41.                 $validExt = false;
  42.                 foreach ($validExtensions as $validExtension) {
  43.                     if ($fileType == $validExtension)
  44.                         $validExt = true;
  45.                 }
  46.                 if (!$validExt) {
  47.                     return 2;
  48.                     $failed = true;
  49.                 }
  50.             }
  51.            
  52.             if (!$failed) {
  53.                 if ($maxSize > 0) {
  54.                     if ($file['size'] > $maxSize) {
  55.                         return 3;
  56.                         $failed = true;
  57.                     }
  58.                 }
  59.                
  60.                 if (!$failed) {
  61.                     if ($isImage) {
  62.                         list($width, $height) = getimagesize($file['tmp_name']);
  63.  
  64.                         if ($minImgWidth > 0) {
  65.                             if ($width < $minImgWidth) {
  66.                                 return 4;
  67.                                 $failed = true;
  68.                             }
  69.                         }
  70.  
  71.                         if ($minImgHeight > 0) {
  72.                             if (!$failed) {
  73.                                 if ($height < $minImgHeight) {
  74.                                     return 4;
  75.                                     $failed = true;
  76.                                 }
  77.                             }
  78.                         }
  79.  
  80.                         if ($maxImgWidth > 0) {
  81.                             if (!$failed) {
  82.                                 if ($width > $maxImgWidth) {
  83.                                     return 5;
  84.                                     $failed = true;
  85.                                 }
  86.                             }
  87.                         }
  88.  
  89.                         if ($maxImgHeight > 0) {
  90.                             if (!$failed) {
  91.                                 if ($height > $maxImgHeight) {
  92.                                     return 5;
  93.                                     $failed = true;
  94.                                 }
  95.                             }
  96.                         }
  97.                     }
  98.                    
  99.                     if (!$failed) {
  100.                         if (!file_exists($newLocation))
  101.                             mkdir($newLocation);
  102.                        
  103.                         if (move_uploaded_file($file['tmp_name'], $targFile))
  104.                             return 0;
  105.                         else
  106.                             return 6;
  107.                     }
  108.                 }
  109.             }
  110.         }
  111.                
  112.         return 0;
  113.     }
Advertisement
Add Comment
Please, Sign In to add comment