Reputation: 136
I want to rename/move a file which I can do OK with the following code but how can I take the next file in the A1 directory and move it to the A2 directory without specifying the file name?
(the files in the A1 directory are numbered from 1.txt to 1000.txt
<?php
rename("/home/vol11_1/htdocs/A1/1.txt", "/home/vol11_1/A2/txt.txt");
?>
Each time the php script is run it should move the next file from the A1 folder to the A2 folder and overwrite the txt.txt file which is already there.
How can this be done?
Thanks
Upvotes: 0
Views: 321
Reputation: 136
I managed to do it with the following code
$from = '/A1';
$files = scandir($from);
$to = '//A2';
if (!empty($files[2])) {
rename("{$from}/{$files[2]}", "{$to}/text.txt");
}
Thanks to all who helped.
Upvotes: 0
Reputation: 1608
EDIT: new version, try this:
<?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");
?>
Upvotes: 2