Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

php - Getting ftp_put progress

I have a php script on a web server that uploads a file to another remote server via ftp_put.

How can I display the current upload progress to the user?

The only similar system I've seen is for file uploads from the user, with ajax requests to check the local size of the uploaded file on the server.

The equivalent system would be ajax requests to the web server, that then checked file sizes on the remote server and returned that data to the user's clientscript.

This seems horribly inefficient to me. Is there a better way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It can be implemented easily using FTP URL protocol wrappers:

$url = "ftp://username:[email protected]/remote/dest/path/file.zip";
$local_path = "/local/source/path/file.zip";

$size = filesize($local_path) or die("Cannot retrieve size file");

$hout = fopen($url, "wb") or die("Cannot open destination file");
$hin = fopen($local_path, "rb") or die("Cannot open source file");

while (!feof($hin))
{
    $buf = fread($hin, 10240);
    fwrite($hout, $buf);
    echo "
".intval(ftell($hin)/$size*100)."%";
}

echo "
";

fclose($hin);
fclose($hout);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...