重命名 php 增量移动文件

rename move files incrementally with php

我想要 rename/move 一个文件,我可以用下面的代码来完成,但是我如何在不指定文件名的情况下将 A1 目录中的下一个文件移动到 A2 目录?

(A1目录下文件编号从1.txt到1000.txt

<?php
rename("/home/vol11_1/htdocs/A1/1.txt", "/home/vol11_1/A2/txt.txt");
?>

每次 php 脚本 运行 它应该将下一个文件从 A1 文件夹移动到 A2 文件夹并覆盖已经存在的 txt.txt 文件。

如何做到这一点?

谢谢

编辑:新版本,试试这个:

<?php

// start new or resume existing session
session_start();

// get the last file number stored on session or 0 at the first time
$last = isset($_SESSION['last']) ? $_SESSION['last'] : 0;

// increment the number
$last++;

// store on session
$_SESSION['last'] = $last;

// move the file
rename("/home/vol11_1/htdocs/A1/$i.txt", "/home/vol11_1/A2/txt.txt");

?>

我设法用下面的代码做到了

$from = '/A1';
$files = scandir($from);

$to = '//A2';
if (!empty($files[2])) {
rename("{$from}/{$files[2]}", "{$to}/text.txt");
}

感谢所有帮助过的人。