获取ftp_put进展 [英] Getting ftp_put progress

查看:167
本文介绍了获取ftp_put进展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过ftp_put上传文件到另一台远程服务器上的Web服务器上的PHP脚本。

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?

我见过的唯一类似的系统是从用户的文件上传,用AJAX请求,检查上传文件的大​​小本地服务器上。

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.

等效系统将是Ajax请求到万维网服务器,该随后检查文件大小在远程服务器上,并返回该数据给用户的clientscript。

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?

推荐答案

如果在其它机器支持FTP服务器 REST 命令(从某一点重新上传)有肮脏的方式实现这一点:

If ftp server on other machine supports REST command (restart uploading from certain point) there is dirty way to implement this:

  1. 创建临时文件
  2. 将X个字节从你要上传的文件,该文件
  3. 上传临时文件
  4. 写状态到另一个文件(或会话,但不知道这是否会)
  5. 添加另一个X字节到临时文件
  6. 在启动形式X字节上传临时文件
  7. crite状态到文件
  8. 在重复5-7,直到整个文件被上传
  9. 删除临时和放大器;状态文件。

样品code:

$fs = filesize('file.bin');
define('FTP_CHUNK_SIZE', intval($fs * 0.1) ); // upload ~10% per iteration

$ftp = ftp_connect('localhost') or die('Unable to connect to FTP server');
ftp_login($ftp, 'login', 'pass') or die('FTP login failed');

$localfile = fopen('file.bin','rb');

$i = 0;
while( $i < $fs )
{
    $tmpfile = fopen('tmp_ftp_upload.bin','ab');
    fwrite($tmpfile, fread($localfile, FTP_CHUNK_SIZE));
    fclose($tmpfile);

    ftp_put($ftp, 'remote_file.bin', 'tmp_ftp_upload.bin', FTP_BINARY, $i);
    // Remember to put $i as last argument above

    $progress = (100 * round( ($i += FTP_CHUNK_SIZE)  / $fs, 2 ));
    file_put_contents('ftp_progress.txt', "Progress: {$progress}%");
}
fclose($localfile);
unlink('ftp_progress.txt');
unlink('tmp_ftp_upload.bin'); // delete when done

和文件,检查使用Ajax:

And file to check with ajax:

if(file_exists('ftp_progress.txt'))
    echo file_get_contents('ftp_progress.txt');
else
    echo 'Progress: 0%';
exit;

这篇关于获取ftp_put进展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆