PHP File Management


Im creating a form which collects user details (name and email), it then fires off some emails and writes the details to a csv file for download at a later date.

The csv file will be added to for a week and then downloaded then a new file will be created.

If multiple users try to open the file and append to it will there be issues???

Any suggestions??

Who is Participating?
m00003643Connect With a Mentor Commented:
Well as firehazrd said, a database solution would be far better if at all possible.  If you expect a lot of traffic, you don't want to be opening a lot of file streams.

Also, if you know you have the only service accessing the .csv, you would probably be better off writing directly to the main csv as you initially intended, but make sure you do it through a singleton "write manager" to prevent concurrent access attempts.

However, to at least answer your question, you could try something like the following.  This code is untested and I'm foggy on a few of these methods, but it should get you close.

Note:  I think  fopen($file, 'r') might need to be changed to fopen($tempDir . $file, 'r').  I can't recall without actually testing the script.
$tempDir = "/csv/temp/";
$mainCSV = "/csv/main.csv";
if (is_dir($tempDir)) {
	if ($d = opendir($tempDir)) {
		while (($file = readdir($d)) !== false) {
			if(isCSV($file)) {
				$fTempHandle = fopen($file, 'r') or die ("Couldn't open temp .csv file!");
				$tempString = fread($fTempHandle, filesize($fTempHandle));
				$fMainHandle = fopen($mainCSV, 'w') or die("Couldn't open main .csv file!");
				fwrite($fMainHandle, $tempString);
function boolean isCSV($file) {
	$extArray = explode('.', $file);
	if(count($extArray) > 1) {
		$fileType = $extArray[count($extArray) -1];
		return ($fileType == "csv");
	} else {
		return false;

Open in new window

Like you suspected, you will get an exception if multiple users try to write to the same file concurrently.

If you want to allow this type of behavior, try the following:
When a user submits a form, fire off your emails like normal, but write the details to a temporary csv (or table for that matter).  Then create a cron job that appends all of the temporary csv's to your main csv every 10/15/20 minutes.

This will insure that no user is ever writing directly to your main csv, which will make it open for writing by the cron job at all times.  Alternatively (if you don't expect a large amount of user submissions), you could simply wait until your weekly reset and append all the separate submissions at that time.
Just use a database... mysql print out your data as a csv later...
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

socrossAuthor Commented:
I would want to use data base but there could be issues with the server the site is run off.

If i create unique files for each submission, how would i use php to open all those files and append them to one main csv file which i could then store in a different location on the server.


* closedir($dh) should also be closedir($d);

CWS (haripriya)Commented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Accept: m00003643 {http:#20835755}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Experts Exchange Cleanup Volunteer
Forced accept.

EE Admin
All Courses

From novice to tech pro — start learning today.