首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >同时对多个FTP的数据处理

问同时对多个FTP的数据处理
EN

Stack Overflow用户
提问于 2015-09-16 18:54:52
回答 2查看 61关注 0票数 1

让我先给您展示一下代码,这样您就可以理解我在说什么:

代码语言:javascript
复制
foreach($video_ftps as $video_ftp) {
  $ftp = new FtpClient($video_ftp[0]);
  $ftp->set_writing_destination($video_ftp[1]);
  if($ftp) {

    if($ftp->create_new_dir('archive'))
      $utils->log("Created directory '/archive' on source.");

    $tmp_ftp = new FtpClient($video_ftp[1]);
    $tmp_ftp->create_new_dir('video');
    $tmp_ftp->create_new_dir('video/'.$video_ftp[2]);
    $tmp_ftp->close();

    // Check if new files arrived on ftp
    if($ftp->has_new_files())
    {
      foreach($ftp->fetch_new_file() as $file) {
        $checksum_source = "";
        $checksum_dest = "";
        if($ftp->copy(MODE_SAME_FTP, $file, 'archive'))
          $checksum_source = sha1_file($video_ftp[0].'/'.$file);
          $utils->log("Copied {$file} to {$ftp->get_writing_destination()}archive/.");

        $fileExtension = explode(".", $file)[1];
        if($fileExtension == "mp4") {
          if($ftp->rename($file , $video_ftp[1].'video/'.$video_ftp[2].'/'.$file))
            $checksum_dest = sha1_file($video_ftp[1].'video/'.$video_ftp[2].'/'.$file);
            if($checksum_source != $checksum_dest) {
              $tmp_ftp = new FtpClient($video_ftp[1]);
              $tmp_ftp->copy(MODE_DIFF_FTP, 'video/'.$video_ftp[2].'/'.$file, $video_ftp[0].'/'.$file);
              $tmp_ftp->delete('video/'.$video_ftp[2].'/'.$file);
              $tmp_ftp->close();
            } else {
              $utils->log("Moved {$file} file to {$ftp->get_writing_destination()}{$video_ftp[1]}'video/'{$video_ftp[2]}/.");
            }
        }
        
      }
    }

    $ftp->close();
  }
}

注意: FTPClient类只执行与所有ftp_*函数相同的操作。该类只是删除前缀并使其面向对象。

所以。把正在发生的事情留在循环中。将其视为FTP正在被操作,文件正在从FTP和不同的FTP中被删除、移动和复制。

你需要了解的一件事是循环。我在$video_ftps中有ftp连接字符串,并顺序地满足每个连接字符串的需要并对其进行操作。

我想要做的是,每个不同的FTP应该在一个不同的线程,并应与其他FTP同时操作。

我读过有关PHP线程的文章,但没有找到或理解与它相关的任何内容。您能告诉我如何将基于循环的东西转换为基于线程的东西吗?

更新1:

我为ftp的操纵部分创建了一个单独的文件:

代码语言:javascript
复制
<?php

require 'FtpClient.php';
require 'Utils.php';

$video_ftp = array($argv[0], $argv[1], $argv[2]);

$ftp = new FtpClient($video_ftp[0]);
$ftp->set_writing_destination($video_ftp[1]);
if($ftp) {

  if($ftp->create_new_dir('archive'))
    $utils->log("Created directory '/archive' on source.");

  $tmp_ftp = new FtpClient($video_ftp[1]);
  $tmp_ftp->create_new_dir('video');
  $tmp_ftp->create_new_dir('video/'.$video_ftp[2]);
  $tmp_ftp->close();

  // Check if new files arrived on ftp
  if($ftp->has_new_files())
  {
    foreach($ftp->fetch_new_file() as $file) {
      $checksum_source = "";
      $checksum_dest = "";
      if($ftp->copy(MODE_SAME_FTP, $file, 'archive'))
        $checksum_source = sha1_file($video_ftp[0].'/'.$file);
        $utils->log("Copied {$file} to {$ftp->get_writing_destination()}archive/.");

      $fileExtension = explode(".", $file)[1];
      if($fileExtension == "mp4") {
        if($ftp->rename($file , $video_ftp[1].'video/'.$video_ftp[2].'/'.$file))
          $checksum_dest = sha1_file($video_ftp[1].'video/'.$video_ftp[2].'/'.$file);
          if($checksum_source != $checksum_dest) {
            $tmp_ftp = new FtpClient($video_ftp[1]);
            $tmp_ftp->copy(MODE_DIFF_FTP, 'video/'.$video_ftp[2].'/'.$file, $video_ftp[0].'/'.$file);
            $tmp_ftp->delete('video/'.$video_ftp[2].'/'.$file);
            $tmp_ftp->close();
          } else {
            $utils->log("Moved {$file} file to {$ftp->get_writing_destination()}{$video_ftp[1]}'video/'{$video_ftp[2]}/.");
          }
      }
      
    }
  }

  $ftp->close();
}

?>

我更新了我以前的文件以运行这个函数,而不是我转移到一个单独文件的代码:

代码语言:javascript
复制
exec("php video_ftp_manipulator.php ". $video_ftp[0] . "" . $video_ftp[1] . "" . $video_ftp[2] . " > /dev/null &");

现在出现的问题是,当我运行我的文件时,什么都没有发生。FTP中没有任何变化。为什么是这样?

EN

回答 2

Stack Overflow用户

发布于 2015-09-17 06:12:57

不需要任何线程处理.

问题是,您对FTP的抽象很差,我建议您放弃它,直接使用扩展。

FTP扩展配备了非阻塞功能,以方便异步请求,使用它们:

这就是你所需要的。

票数 3
EN

Stack Overflow用户

发布于 2015-09-16 20:00:47

PHP本质上不执行异步请求,尽管有一些方法可以通过在PHP中使用exec()或shell_exec()触发单独的子脚本来完成模拟多线程,将参数传递给子脚本,并将它们的输出发送到/dev/null,这样PHP就不会在继续之前等待响应。虽然可能,但并不理想(我也不推荐它),因为没有从子脚本中获取有用数据的有效方法。

可以在循环中使用exec()从shell命令触发另一个脚本。示例使用:exec("php child.php argument1 argument2 > /dev/null &");

在这里,我将child.php作为一个shell脚本运行,将其传递给它的参数,可能会有额外的参数。我将输出发送到/dev/null,这样php就不会等待它了。

在我的子PHP脚本中,我需要使用以下方法获得参数字符串:$first_arg = $argv[1]; $second_arg = $argsv[2];

参数可以是ftp服务器和文件名,也可以添加参数来指定子脚本要采取的操作.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32616542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档