【问题标题】:Move/copy and rename most recently modified file移动/复制和重命名最近修改的文件
【发布时间】: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 作业运行它
  • 您能否更新/编辑您的问题,而不是在评论中添加代码?
  • 抱歉,这是我在这里发布的第一个问题。不知道它是如何工作的

标签: php rename


【解决方案1】:

我没有测试它,但我认为这应该可以工作(比较你的 ftp 脚本):

<?php
$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
$sourceDir = "./data";
$destDir = "./destinationfolder";
if (!is_dir($sourceDir) || !is_dir($destDir)) {
    exit("Directory doesn't exists.");
}
if (!is_writable($destDir)) {
    exit("Destination directory isn't writable.");
}
$mostRecentFilePath = "";
$mostRecentFileMTime = 0;
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sourceDir), RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile()) {
        if ($fileinfo->getMTime() > $mostRecentFileMTime) {
            $mostRecentFileMTime = $fileinfo->getMTime();
            $mostRecentFilePath = $fileinfo->getPathname();
        }
    }
}
if ($mostRecentFilePath != "") {
    if (!rename($mostRecentFilePath, $destDir . "/file1.xml")) {
        exit("Unable to move file.");
    }
}
?>

【讨论】:

  • 非常感谢您。我确实将 !rename 更改为 !copy 所以我可以保留原件。但除此之外,它也完全按照我的需要工作。非常感谢您的帮助。
  • 请不要添加带有感谢的评论,而是请投票支持我的解决方案。提前致谢。 ??????? 和✔如果您的问题不需要更多帮助。
  • 我的名声还不够好,我的选票还不能被看到
猜你喜欢
  • 2016-05-23
  • 1970-01-01
  • 2017-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
相关资源
最近更新 更多