简体   繁体   English

在服务器目录中查找最新的JSON文件

[英]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. 我想最好使用JQuery / javascript甚至PHP在指定目录中查找最新的JSON文件。

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. 因为当我的html5 / javascript网络应用程序的用户将一些对象数组保存到JSON文件时,除了原始工作JSON文件之外,还有一个备份文件,该备份文件是用随机名称创建的,并且是原始JSON的精确副本文件。

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 . 如果原始JSON文件发生问题, 我希望用户能够从备份目录中打开最新的备份文件,然后选择要恢复的文件

To open a JSON file I usually use this code: 要打开JSON文件,我通常使用以下代码:

$.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. 现在的麻烦是,在备份的情况下,我不知道应该打开的JSON文件的名称,因为每个文件都是unic,并且在创建时具有Math.random()生成的名称。

So I repeat the question: Is there a way to open from the backups directory the most recently created, randomly named, JSON files? 因此,我重复这个问题: 是否可以从备份目录中打开最近创建的,随机命名的JSON文件?

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. 如果没有,我可能会尝试使用.getTime() javascript方法而不是Math.random()来控制创建的备份文件的名称,然后使用循环搜索有效的备份文件名。 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. 如果您的服务器在main\\backups文件夹中支持WebDav或FTP,则可以搜索今天更大的所有文件,然后选择最新的文件。

--- Addition --- -加法-

With PHP, take a look at sort files by date in PHP 使用PHP,看看PHP中按日期对文件进行排序

You could replace your $.getJSON() call with a standard ajax request: 您可以将$.getJSON()调用替换为标准的ajax请求:

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

getMostRecentBackup.php 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 getMostRecentBackup.php将读取备份目录并返回一个包含最新备份文件的JSON对象,请阅读此主题: PHP中按日期对文件排序

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

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