View Full Version : put number of data files on server into javascript variable

02-17-2012, 08:53 PM

Hopefully this is a pretty basic questions, but my level of php knowledge is even more basic at this point...

On my website, a user enters at Page 1, completes a short game on Page 2, and is fare-welled on Page 3. Also on Page 3, a unique text file is created with data and stored on the server in a folder called "data".

Thus, the number of text files in the folder "data" is represents a count of how many users have made it to Page 3.

I would like to know how many users have made it to Page 3 on Page 1. Thus, I was thinking that there might be some php code I could use on Page 1 to search in the server side folder called "data", count how many text files there are, and then create a javascript variable with that number.

Any help would be very appreciated.


02-18-2012, 03:56 PM
Off the top of my head I don't know how to count files in a folder. I suppose there is a way and I'm sure someone will enlighten us, but in the mean time....

Things would be a lot simpler if you used a database to keep your information, the number of files could increase into the thousands. My suggestion is to have a file that contains a number. That number is the number of files in your folder. When you reach page three, open that file and set a variable to that number and add 1 to it. Then when you store your file on the user over write the number file. It will always have the latest and truest count of people that have made it to page three and can be accessed from page one.

02-18-2012, 04:24 PM
$directory = "../datafiles/";
if (glob($directory . "*.txt") != false)
$filecount = count(glob($directory . "*.txt"));
echo $filecount;
echo 0;