让我先给您展示一下代码,这样您就可以理解我在说什么:
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的操纵部分创建了一个单独的文件:
<?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();
}?>
我更新了我以前的文件以运行这个函数,而不是我转移到一个单独文件的代码:
exec("php video_ftp_manipulator.php ". $video_ftp[0] . "" . $video_ftp[1] . "" . $video_ftp[2] . " > /dev/null &");现在出现的问题是,当我运行我的文件时,什么都没有发生。FTP中没有任何变化。为什么是这样?
发布于 2015-09-17 06:12:57
发布于 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服务器和文件名,也可以添加参数来指定子脚本要采取的操作.
https://stackoverflow.com/questions/32616542
复制相似问题