繁体   English   中英

用php逐步重命名移动文件

[英]rename move files incrementally with php

我想重命名/移动一个文件,可以使用以下代码来完成该操作,但是如何在不指定文件名的情况下将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");
}

感谢所有的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM