简体   繁体   中英

How to iteratively get ftp folder size

I have written a code to get the size of a selected folder along with all sub folders, but the issue I am facing is, not able to increment size counter in the loop,

function getsize($username,$password,$server,$dir){
    $connid = ftp_connect($server);

    //ini_set('xdebug.max_nesting_level',500);
    $connid = ftp_connect($server);
    $login_result = ftp_login($connid, $username, $password);
    $total_size = 0;
    $contents_on_server = ftp_nlist($connid, $dir);
    foreach($contents_on_server as $user_file) {
        if(ftp_size($connid,$user_file) == -1){
            $directory = $user_file;
            $obj = new FTP();
            $obj->getsize($username,$password,$server,$directory);  
        }
        else{
            //$file_size = ftp_size($connid,$user_file);
            $total_size += ftp_size($connid,$user_file);
        }
    }
    echo $total_size;

}"

If I print $total_size , it outputs as 192944122391740 , the size of all three files appended but not summed.

Any help??

You are iterating on same function getsize which is resetting your $total_size to zero causing issue. Create a setsize object in your class, set value on each iteration, reset object on final call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM