0

I have a complicated problem here and after months of research, I still couldn't find a more convenient way of doing this.

I have a website that reads JSON text files and displays the info from that JSON file to the webpage as table. The JSON file contains fields such as date, action, user, etc.

The way I currently do it is I added a File Upload HTML tag so the user can select the JSON file to read. I have no choice but to do File Upload as I see no other ways but I'd really prefer not to upload these files to the server, I just need to read the contents. Anyway, after uploading the file and reading the contents into a variable, I then have to store these files to MySql database just so I can sort them by date, I have to be able to display the table sorted by date.

I have 2 questions;

  • Is there any other way to read text file contents without uploading the file to read?
  • Is there some sort of simple way to sort an array (by date)?
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
Jay
  • 590
  • 6
  • 13
  • 29
  • possible duplicate of [PHP order array by date?](http://stackoverflow.com/questions/6401714/php-order-array-by-date) – the1dv Feb 10 '15 at 10:19
  • `uasort()` to sort. Upload the file to `/tmp` and your OS will clear it automatically - or use pastebin API and `file_get_contents` (depending on what information these files hold). Although, I believe this question is offtopic for opinionated answers. – ʰᵈˑ Feb 10 '15 at 10:19
  • 4
    Am I getting this right, after __months__ of research the best you could do was to save arrays to mySQL so you could order them by date when querying? You didn't stumble upon uasort() and similar functions in your months of research? – Sejanus Feb 10 '15 at 10:23
  • If you need to read data on the server, you need that data to be uploaded; there is no difference between uploading a file, and transmitting the contents of that file. If you only need to redisplay the data on the user's browser, without the server ever needing to know the contents, then you could potentially do the whole thing in JS using the File API (see [MDN](https://developer.mozilla.org/en-US/docs/Web/API/File) or [WPD](https://docs.webplatform.org/wiki/apis/file)). – IMSoP Feb 10 '15 at 10:38

3 Answers3

3

1) You can read file on users local computers, but that must be done with JavaScript and has a lot of limitations. http://dev.w3.org/2006/webapi/FileAPI/

I would recommend uploading file anyway, do what you want and then delete (by unlink()) the file.

2) PHP order array by date?

Community
  • 1
  • 1
Forien
  • 2,712
  • 2
  • 13
  • 30
1

1) There is a way to read files using Javascript File Reader. Maybe you would find this link helpful. If you opt to not use Javascript, then the file will have to be uploaded on the server before reading it. You may upload it to a temporary location and delete it once you have read the contents.

2) Other than the ideas given in other answers, you might want to have a look at PHP's array_multisort function.

v2solutions.com
  • 1,439
  • 9
  • 8
0

Why not storing the JSON data directly into the database ? You have not to upload the file (just do a sql insert), you can add the date and sort it...

Matthieu
  • 948
  • 8
  • 8