简体   繁体   中英

Find most recent JSON file in a server directory

I can't really match an answer to this issue so I've decided to post this question hoping to find a solution.

I would like to use preferably JQuery / javascript or even PHP to find the most recent JSON files in a specified directory.

Why I would like to do this? Because when a user of my html5/javascript webapplication saves some array of objects to a JSON file, then besides the original work JSON file, there is a backup file that is created with a random name and which is the exact copy of the original JSON file.

If something happens to the original JSON file I would like the user to be able to open the most recent backup files from the backups directoy and select the right one to be recovered .

To open a JSON file I usually use this code:

$.getJSON('main/backups/random1345004.json', function(info){ ... });

Now the trouble is that in case of a backup I don't know the name of the JSON file that should be opened, because each file is unic and has a Math.random() generated name when it's created.

So I repeat the question: Is there a way to open from the backups directory the most recently created, randomly named, JSON files?

If not I might try to use .getTime() javascript method instead of Math.random() to control the names of the backup files created and then use a loop to search for a valid backup file name. That's a hunch, but I would not like to make anything stupid if there is a better solution, without loops.

Security is not a concern for me at this rate.

Thank you for any help provided!

If your server supports WebDav or FTP on your main\\backups folder then you could search for all files greater today and then select the more recent.

--- Addition ---

With PHP, take a look at sort files by date in PHP

You could replace your $.getJSON() call with a standard ajax request:

<script>
$.ajax({
    url      : "getMostRecentBackup.php",
    datatype : "json"
})
.done(function(data){
    console.log( data.toSource() );
})
.fail(function() {
    alert( "error" );
});
</script>

will read backup directory and return a JSON object containing the most recent backup file, please read this Topic: sort files by date in PHP 将读取备份目录并返回一个包含最新备份文件的JSON对象,请阅读此主题: PHP中按日期对文件排序

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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