-2

I´m running a code that saves the files submitted by users into a directory. I would like to keep track of the files submitted, specially the date, day of the week, the time, etc.

I want to be able to know at which day of the week, at which time of the day, which month, etc, the files submission has the higher rate, the current amount of files submited, etc. Of course plotting the data to a graph would be amazing.

I understand a simple Excel sheet can be created with phpExcel or something of the sort and this can be achieved by these means.

But I don't like the idea of having infinite rows with the data in them.

I haven't been able to find anything simpler or different rather than Excel sheets.

Does anyone happen to know a different method to record these data?

I´ sorry I forgot to mention, I´m not interested on keeping the database online, local will be okay.

ScottMcGready
  • 1,612
  • 2
  • 24
  • 33
  • Save it to a database with a timestamp and you'll be sweet. (ps, `time()` gives you a unix timestamp). Then all you need to do is add some logic to your data retrieval. – Darren Sep 03 '15 at 01:21
  • Why don't you either search the logs? (You do have logs don't you???) or alternatively every file upload, add this to a new db table with date stamp, user ID, file name, and anything else you think is relevant. Then you can query it – ScottMcGready Sep 03 '15 at 01:21
  • @ScottMcGready what kind of database is recommendable? any examples I could look at? – Emilio Basualdo Cibils Sep 03 '15 at 01:23
  • @PiloBasualdo MySQL is probably the easiest to setup and use. Hang on I'll add an answer with some more information – ScottMcGready Sep 03 '15 at 01:24
  • ok, thanks, and without using MySQL? – Emilio Basualdo Cibils Sep 03 '15 at 01:32
  • @PiloBasualdo haha no. I guess you could log the stuff to a text file and then attempt to track it that way but believe me, a database is **by far** the most efficient way to do it. – ScottMcGready Sep 03 '15 at 01:39

1 Answers1

2

When a user submits a file to their directory, in the upload script, make a call to a function like below:

function log_upload($user_id, $filepath){
    // check if user ID is valid using your own logic

    // Connect to your database
    $mysqli = new mysqli('host', 'user', 'pass', 'db_name');

    // Check connection is successful
    if($mysqli->connect_error){
        die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); 
    }

    $user_id = '"'.$mysqli->real_escape_string($user_id).'"';
    $datestamp = '"'. date("Y-m-d H:i:s") .'"';
    $filepath = '"'.$mysqli->real_escape_string($file_path).'"';


    $insert_stmt  = $mysqli->query("INSERT INTO logs (user_id, datestamp, file_path) VALUES ($user_id, $datestamp, $filepath)");
    if(!$insert_stmt){
        die('Error : ('. $mysqli->errno .') '. $mysqli->error);
    }
}

In your upload script call log_upload($user_id,$filepath); after it's uploaded successfully.

Note: You'll need to add your own logic or figure out how to pass the user_id and filepath of uploaded file yourself.

When you want to query this data, a simple MySQL query such as:

SELECT user_id, COUNT(user_id) as user_counted 
FROM logs 
WHERE datestamp BETWEEN '2015-01-01' 
AND '2015-01-31' 
GROUP BY user_id

Would return the highest using users on your site for the month January in 2015.

You can start to build up more complex SQL queries to determine other factors as you require them. Have a look at the MySQL manual for guidance and tips, also (obviously!) Stack Overflow.

If you absolutely want to view this data using Excel, you can download an Excel/CSV file from MySQL and use Excel until your comfort level with SQL queries increases. There's loads of SQL tutorials out there and this Stack Overflow question, although a bad question, does touch on how to generate graphs etc. from MySQL.

Community
  • 1
  • 1
ScottMcGready
  • 1,612
  • 2
  • 24
  • 33
  • Thanks for the help. Do you know any other database creation either than MySQL? – Emilio Basualdo Cibils Sep 03 '15 at 01:41
  • @PiloBasualdo Why are you so against MySQL out of interest? It's one of the most widely known databases out there and I guarantee if you're running PHP somewhere, you have it. Also, how are you doing this user validation if you're not using a database? – ScottMcGready Sep 03 '15 at 01:42
  • There's also SQLite to consider but the theory would be the same. It stores things on a file and I wouldn't recommend that for a public facing website unless you know where to store it securely. – ScottMcGready Sep 03 '15 at 01:44
  • I´ sorry I forgot to mention, I´m not interested on keeping the database online, local will be okay. – Emilio Basualdo Cibils Sep 03 '15 at 01:52
  • @PiloBasualdo ???? Sorry, that doesn't make sense. You'll need to store the data **somewhere** when a user uploads a file. Unless you want to start setting up some local API and calling that from your website each time a file is uploaded. Honestly, it's less resource intensive to set up a db on your live site that is only used for logging (as suggested). – ScottMcGready Sep 03 '15 at 01:54
  • 1
    This is the way you should be doing it as Scott has stated. Storing it "file" based is not a smart way of doing it. MySQL is coupled with PHP almost everywhere, and for good reason. @PiloBasualdo You're trying to over complicate it and be "smart" about it. Go with the tried and tested method. – Darren Sep 03 '15 at 01:54
  • @PiloBasualdo You're idea of storing things in an Excel sheet will consume **way more memory** and potentially lead to a bottleneck on your server. Plus you'll need to figure out a way of rotating the Excel log periodically which won't be fun. Using a database is by far the most efficient and easiest way. Also it gives you loads of options in the future to query the data and you can pull it down by doing a `mysql_dump` and importing it to a local database for querying. – ScottMcGready Sep 03 '15 at 01:59
  • All right then. I shall use MySQL. I suppose there is a way of periodically creating graphs comparing data and stuff, right? – Emilio Basualdo Cibils Sep 03 '15 at 02:02
  • @PiloBasualdo You can kind of do whatever you want really. You can even export the MySQL data to Excel/CSV if you want to use it in Excel until your querying knowledge get's better. And I just spotted your username and realised you're the same guy from last night :). Saor Alba. – ScottMcGready Sep 03 '15 at 02:04
  • @PiloBasualdo You're welcome! Please don't forget to mark the answer accepted which helped most in solving the problem. See also [How does accepting an answer work?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). Also, please don't forget to do the same for questions which you asked previously, whenever applicable. You can find them in [your profile](http://stackoverflow.com/users/5076247/pilo-basualdo). If no answer is applicable and you have already solved the problem by yourself, then you're eligible to post it as an answer and accept it. Good luck! – ScottMcGready Sep 03 '15 at 02:15