Ronald Dewindt
Ronald Dewindt

Reputation: 65

PHP - get latest time() item made

I made a backup folder with time().json items, so that when something goes wrong with the real file i can back it up with the latest saved time().json.

I already made the Json files like this.

$backup=fopen("backup/".time().".json", "w");
fwrite($backup, json_encode($list));
fclose($backup);

now what i need is to make a code that when i press backup it changes list.json with the latest time().json file made.

Does someone have an idea. I was thinking on something like this.

$backupFile = file_get_contents("**lastest file here**");
                   file_put_contents('list.json', $backupFile);

The only thing is i dont get how to select the last item made with time().json.

Upvotes: 1

Views: 88

Answers (2)

Leo
Leo

Reputation: 129

exec("cp -f `ll -lstr backup | tail -n 1 | awk '{print $NF}'` list.json")

Upvotes: 0

M. Eriksson
M. Eriksson

Reputation: 13635

I would save the latest filename in a file called "latest.txt" or something. No need to iterate through the files.

$filename = time() . '.json';
$backup=fopen("backup/".$filename, "w");
fwrite($backup, json_encode($list));
fclose($backup);

file_put_contents('latest.txt', $filename);

Then when you need to get the latest file:

$filename   = file_get_contents('latest.txt');
$backupFile = file_get_contents($filename);
file_put_contents('list.json', $backupFile);

Upvotes: 2

Related Questions