【发布时间】:2021-02-21 22:21:34
【问题描述】:
我有一个文件夹,其中包含一些随机命名的文件,其中包含我需要的数据。
为了使用数据,我必须将文件移动到另一个文件夹并将文件命名为“file1.xml”
每次移动和重命名文件时,它都会替换目标文件夹中以前的“file1.xml”。
源文件夹包含所有其他随机命名的文件,这些文件作为存档保存。
php 将每 48 小时通过一个 cron 作业运行一次
以下工作,但它是用于 ftp。我需要为本地文件编辑它。
$conn = ftp_connect('ftp.source.com'); ftp_login($conn, 'username', 'password'); // get file list $files = ftp_nlist($conn, '/data'); $mostRecent = array( 'time' => 0, 'file' => null ); foreach ($files as $file) { // get the last modified time $time = ftp_mdtm($conn, $file); if ($time > $mostRecent['time']) { // this file is the most recent so far $mostRecent['time'] = $time; $mostRecent['file'] = $file; } } ftp_get($conn, "/destinationfolder/file1.xml", $mostRecent['file'], FTP_BINARY); ftp_close($conn);
【问题讨论】:
-
那么您是否想创建一个 php 脚本作为 cronjob,每 48 小时将最新文件从一个文件夹复制到另一个文件夹并覆盖那里的文件?到目前为止,您尝试过什么?
-
$conn = ftp_connect('ftp.source.com'); ftp_login($conn, '用户名', '密码'); // 获取文件列表 $files = ftp_nlist($conn, '/data'); $mostRecent = array('time' => 0, 'file' => null); foreach ($files as $file) { // 获取上次修改时间 $time = ftp_mdtm($conn, $file); if ($time > $mostRecent['time']) { // 这个文件是目前最新的 $mostRecent['time'] = $time; $mostRecent['file'] = $file; } } ftp_get($conn, "/destinationfolder/file1.xml", $mostRecent['file'], FTP_BINARY); ftp_close($conn);
-
以上工作。但它是用于 ftp 的。我有点菜鸟,我正在尝试将其转换为适用于本地文件而不是远程文件。我在 cpanel 上运行,所以一旦我有一个 php 脚本,我就会通过 cpanel cron 作业运行它
-
您能否更新/编辑您的问题,而不是在评论中添加代码?
-
抱歉,这是我在这里发布的第一个问题。不知道它是如何工作的