Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Check Folder Empty
- function checkFolderHasFiles ( $folderName ){
- $files = array ();
- if ( $handle = opendir ( $folderName ) ) {
- while ( false !== ( $file = readdir ( $handle ) ) ) {
- if ( $file != "." && $file != ".." )
- $files[] = $file;
- if(count($files) >= 1)
- break;
- }
- closedir ( $handle );
- }
- return ( count ( $files ) > 0 ) ? TRUE: FALSE;
- }
- //Delete files or directory
- function deleteDir($dirPath)
- {
- if (!is_dir($dirPath)) {
- if (file_exists($dirPath) !== false) {
- unlink($dirPath);
- }
- return;
- }
- if ($dirPath[strlen($dirPath) - 1] != '/') {
- $dirPath .= '/';
- }
- $files = glob($dirPath . '*', GLOB_MARK);
- foreach ($files as $file) {
- if (is_dir($file)) {
- deleteDir($file);
- } else {
- unlink($file);
- }
- }
- rmdir($dirPath);
- }
- //Get folder size
- function FolderSize($dir) {
- $fSize = 0;
- foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $f){
- $size+=$f->getSize();
- }
- return $fSize;
- }
- //Convert bytes to MB
- function bytesToMB($file)
- {
- $filesize = (filesize($file) * .0009765625) * .0009765625; // bytes to MB
- return round($filesize, 2);
- }
- //Update free space accordingly
- function updateFreeSpace(string $isAddition){
- //Get the size of the file and update the available free space in the database
- $freeSpace = $_SESSION['freeSpace'];
- if($isAddition == "addition")
- $freeSpace = $freeSpace + bytesToMB(FolderSize('uploads/'.$_SESSION['username'].'/'.$fileName));
- else
- $freeSpace = $freeSpace - bytesToMB(FolderSize('uploads/'.$_SESSION['username'].'/'.$fileName));
- $_SESSION['freeSpace'] = $freeSpace;
- $username = $_SESSION['username'];
- $query = "UPDATE users
- SET freeSpace = $freeSpace
- WHERE username = '$username'";
- require 'dbCon.php';
- mysqli_query($db, $query);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment