PHP在特定时间移动文件
我正在尝试在特定时间将文件从一个文件夹移动到另一个文件夹。为了实现这一点,我尝试将Linux at命令与管道一起使用:PHP在特定时间移动文件,php,pipe,Php,Pipe,我正在尝试在特定时间将文件从一个文件夹移动到另一个文件夹。为了实现这一点,我尝试将Linux at命令与管道一起使用: `mv file /to/dest | at h:m d.m.y` 这是我写的: $move = "mv $filename /destination/folder"; $at = "at $my_datetime"; $res = execute_pipe($move,$at); 其中,execute_pipe函数定义如下: function execute_pipe($
`mv file /to/dest | at h:m d.m.y`
这是我写的:
$move = "mv $filename /destination/folder";
$at = "at $my_datetime";
$res = execute_pipe($move,$at);
其中,execute_pipe函数定义如下:
function execute_pipe($cmd1 , $cmd2)
{
$proc_cmd1 = proc_open($cmd1,
array(
array("pipe","r"), //stdin
array("pipe","w"), //stdout
array("pipe","w") //stderr
),
$pipes);
$output_cmd1 = stream_get_contents($pipes[1]);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
$return_value_cmd1 = proc_close($proc_cmd1);
$proc_cmd2 = proc_open($cmd2,
array(
array("pipe","r"), //stdin
array("pipe","w"), //stdout
array("pipe","w") //stderr
),
$pipes);
fwrite($pipes[0], $output_cmd1);
fclose($pipes[0]);
$output_cmd2 = stream_get_contents($pipes[1]);
fclose($pipes[1]);
fclose($pipes[2]);
$return_value_cmd2 = proc_close($proc_cmd2);
return $output_cmd2;
}
问题是文件会立即移动,忽略
at
命令。我错过了什么?有更好的方法吗?对我来说,您的问题似乎与PHP无关。你只是用错了外壳。
位于的手册页内容如下:
但是shell的使用会执行“mv file/destination”命令,然后通过管道将该命令的输出传输到的。在成功的移动操作中,输出将为零。因此,通过使用管道,您实际上可以立即移动文件,并告诉at
在指定的时间不要执行任何操作
在终端中键入man at
以解决问题,从而阅读at
的手册页。提示:如果您想使用STD输入,回显您的命令可能会有所帮助;) 另一个解决方案是cronjob,它每分钟执行一次php脚本。然后脚本检查是否有东西需要移动。不过听起来更复杂,不是吗?无论如何,谢谢你!如果没有解决办法,我一定会同意你的建议!将其作为CRON作业来完成,将是您尝试使用PHP来完成它的唯一方法。另一种(也是可怕的)方法是创建一个PHP脚本,它永远循环,永远不会超时,并执行相同的操作,但这将消耗一个线程来完成一些实际上不需要的事情。
at and batch read commands from standard input or a specified file
which are to be executed at a later time, using /bin/sh.