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

Categories

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

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

1 Answer

It can be implemented easily using FTP URL protocol wrappers:

$url = "ftp://username:password@ftp.example.com/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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...