1

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!

Laci
  • 566
  • 2
  • 15
  • 39

2 Answers2

2

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

Community
  • 1
  • 1
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • Yes. I use FTP to connect to my server but I don't understand how can a user search for the files through my web application? I wont't give them full FTP access. – Laci Apr 26 '14 at 11:36
  • You could create an `anonymous` that only allows directory read access to the specific folder. The FTP scheme allows username and password, `ftp://anonynous:nothing@host/...` – Richard Schneider Apr 26 '14 at 11:39
  • No. It has to be done through the webapplication with javascript/ JQuery or PHP to have a nice flow in the page even if the JSON has to be recovered. – Laci Apr 26 '14 at 11:43
  • Well your question asked `is there any way` and `security is not a concern`. – Richard Schneider Apr 26 '14 at 11:45
  • Under `security is not a concercn` I've meant the use of client side solutions such as JQuery get(). And is there any way within JQuery, javascript, PHP, html5 frames. You're right, sorry if I wasn't clear enough. – Laci Apr 26 '14 at 12:04
  • 1
    There's a javascript library for WebDAV http://ajaxian.com/archives/javascript-webdav-client – Richard Schneider Apr 26 '14 at 12:05
  • Your --- Addition --- solved my problem. It's exactly what I needed: http://stackoverflow.com/a/2667105/1863061 . Thank you! – Laci Apr 27 '14 at 10:57
2

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>

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

Community
  • 1
  • 1