dasun101

Untitled

Dec 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1. <?php
  2.  
  3.     //Check Folder Empty
  4.     function checkFolderHasFiles ( $folderName ){
  5.         $files = array ();
  6.         if ( $handle = opendir ( $folderName ) ) {
  7.             while ( false !== ( $file = readdir ( $handle ) ) ) {
  8.                 if ( $file != "." && $file != ".." )
  9.                     $files[] = $file;
  10.                 if(count($files) >= 1)
  11.                     break;
  12.             }
  13.             closedir ( $handle );
  14.         }
  15.         return ( count ( $files ) > 0 ) ? TRUE: FALSE;
  16.     }
  17.  
  18.     //Delete files or directory
  19.     function deleteDir($dirPath)
  20.     {
  21.         if (!is_dir($dirPath)) {
  22.             if (file_exists($dirPath) !== false) {
  23.                 unlink($dirPath);
  24.             }
  25.             return;
  26.         }
  27.  
  28.         if ($dirPath[strlen($dirPath) - 1] != '/') {
  29.             $dirPath .= '/';
  30.         }
  31.  
  32.         $files = glob($dirPath . '*', GLOB_MARK);
  33.         foreach ($files as $file) {
  34.             if (is_dir($file)) {
  35.                 deleteDir($file);
  36.             } else {
  37.                 unlink($file);
  38.             }
  39.         }
  40.  
  41.         rmdir($dirPath);
  42.     }
  43.  
  44.     //Get folder size
  45.     function FolderSize($dir) {
  46.         $fSize = 0;
  47.         foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $f){
  48.             $size+=$f->getSize();
  49.         }
  50.     return $fSize;
  51.     }
  52.  
  53.     //Convert bytes to MB
  54.     function bytesToMB($file)
  55.     {
  56.        $filesize = (filesize($file) * .0009765625) * .0009765625; // bytes to MB
  57.        return round($filesize, 2);
  58.     }
  59.  
  60.     //Update free space accordingly
  61.     function updateFreeSpace(string $isAddition){
  62.         //Get the size of the file and update the available free space in the database
  63.             $freeSpace = $_SESSION['freeSpace'];
  64.             if($isAddition == "addition")
  65.                 $freeSpace = $freeSpace + bytesToMB(FolderSize('uploads/'.$_SESSION['username'].'/'.$fileName));
  66.             else
  67.                 $freeSpace = $freeSpace - bytesToMB(FolderSize('uploads/'.$_SESSION['username'].'/'.$fileName));
  68.  
  69.             $_SESSION['freeSpace'] = $freeSpace;
  70.  
  71.             $username = $_SESSION['username'];
  72.             $query = "UPDATE users
  73.                      SET freeSpace = $freeSpace
  74.                      WHERE username = '$username'";
  75.  
  76.             require 'dbCon.php';
  77.             mysqli_query($db, $query);
  78.     }
  79.  
  80.  
  81.  
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment