Limit storage space per user in Laravel

0

I am working on a new project and I was wondering if it is possible to limit the storage space per user in Laravel

Let me explain, what I'm looking for is that if the user has the role student then he can store up to 1GB in his profile.

And if there is a user of type teacher will have unlimited space or another type of amount of space like 4GB ... And so on. I've been researching and testing for several weeks but I still can not find a solution that really contributes to what I'm looking for.

All suggestions are welcome. Thank you very much

    
asked by Brayan Angarita 09.11.2018 в 20:54
source

1 answer

1

You can do it by creating a folder for each user: and calculating the space used if it exceeds it does not allow you to upload more files;

<?php 


function formatBytes2($size, $precision = 0){
    $unit = ['Byte','KB','MB','GB','TB','PB','EB','ZB','YB'];

    for($i = 0; $size >= 1024 && $i < count($unit)-1; $i++){
        $size /= 1024;
    }
    return array(round($size, $precision),$unit[$i]);
}

function GetDirectorySize($path){
    $bytestotal = 0;
    $path = realpath($path);
    if($path!==false && $path!='' && file_exists($path)){
        foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object){
            $bytestotal += $object->getSize();
        }
    }

    return formatBytes2($bytestotal);
}
//ejemplo a 100MB
$max_size= array(100,"MB");
$path = __DIR__ . DIRECTORY_SEPARATOR;
$size = GetDirectorySize($path."jhon");
$total = round($max_size[0]-$size[0],2);

if ($size[0] >= $max_size[0] && $size[1] == $max_size[1]) {
    echo "as alcanzao del limite de espacio ya no puedes subir mas archivos";
}else{
    echo "actualmente tienes: {$size[0]} {$size[1]} en uso, restan: {$total} {$size[1]} de {$max_size[0]} {$size[1]}";
}
?>
    
answered by 09.11.2018 в 21:31