[thelist] php writing to a file

Ray Hill lists at prydain.com
Thu May 23 04:35:01 CDT 2002


> I need for each different user to be able to write to a different
> file and inparticular to be able to identify with that file later
> specifically in assocaition with the user who submitted the info

Ok, so you would use a naming convention like fileprefix_userid.txt,
and assign the name of the file you're writing to as a variable, so
that you can plug in the userid from the session.  For example,
assuming that your session has already set a variable called $userid,
you would tweak your earlier code to the following:

   $filename = "userinfo_" . $userid . ".txt";
   $data = "whatever";
   $fp = fopen($filename,"w");
   fwrite($fp,$data);
   fclose($fp);

I'm doing the same thing for some automatic log files right now, so
that the system writes a new file for each day, rather than
overloading a single log file (adds up fast when you're getting
1500-2500 entries a day).  So instead of having a single log file that
took ages to open, like it used to, I now have a collection of small
files all using the naming convention of success_YYYY-MM-DD.txt.
Voila!


Of course, you are still going to have to make sure that the files
you're writing to are not accessible from the web server, or that they
don't contain any privileged information, since this method would
otherwise make it really easy to get to other people's info just by
having their userid.  Or, as Steve suggest, go the database route.

--ray





More information about the thelist mailing list