[thelist] php writing to a file

Steve Lewis slewis at macrovista.net
Thu May 23 13:24:00 CDT 2002


Andrew Maynes wrote:


> $string = implode("\t",$_POST);
> $fp = fopen("file.txt","w");
> fwrite($fp,$product);
> fclose($fp);


> However, this aint a solution.  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 :) does
> that make sense?

If you want to specify your own filename, replace file.txt with a
variable.  I probably misunderstand.  If you need an arbitrary filename
use tempnam() [ http://www.php.net/manual/en/function.tempnam.php ]
which was designed for this purpose.

It looks like you want to record that specified filename in the session
for that user, so they use the same file throughout the visitor's time
on your website.

> The code above
> will not recognise individual users it will only overwrite anything submitted
> rfom the form!
If you want to add new data to an existing file you should open the file
in append mode, not write mode.
[ http://www.php.net/manual/en/function.fopen.php ]

 > do you think it would be easy to convert to code and csv files to a
 > database
 > solution?  I reckon it means starting it all again?
I heartily agree that you should seriously consider using a database
rather than csv files.  Most database systems allow data to be imported
from a cvs file so you should have little problems with migration.

--Steve




More information about the thelist mailing list