Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP FTP Amount Transfered

Tags:

php

Is is possible to detect how much data has been transfered using PHP's FTP module?

Pseudo Code

... connect to server
ftp_nb_put(file...)
while(true) {
    $data = ftp_nb_continue(file...);
    if ($data === FTP_MOREDATA) {
        continue ... get amount transfered ...
    } else {
        break ... check if finished etc ...
    }
}
like image 977
Nick Avatar asked Dec 03 '25 23:12

Nick


1 Answers

Your probably got an answer by now, but for anyone searching... This is an ftp upload function with progress callback. $lcfn = local filename $rmfn = remote filename

function ftp_upload($conn, $lcfn, $rmfn, $progress)
{
    $ret = false;

    $_pc = -1;
    $totalBytes = filesize($lcfn);

    $fp = fopen($lcfn, 'rb');

    $state = @ftp_nb_fput($conn, $rmfn, $fp, FTP_BINARY);

    if($state !== FTP_FAILED){

        while($state === FTP_MOREDATA){

            $doneSofar = ftell($fp);
            $percent = (integer)(($doneSofar / $totalBytes) * 100);

            if($_pc != $percent){
                $progress($percent);
                $_pc = $percent;
            }

            $state = @ftp_nb_continue($conn);
        }

        if($state === FTP_FINISHED){

            if($_pc != 100){
                $progress(100);
            }

            $ret = true;

        }else{
            //error: not finished
        }
    }else{
        //error: failure
    }

    fclose($fp);
    return $ret;
}
like image 75
Rbgo Web Avatar answered Dec 06 '25 13:12

Rbgo Web