PHP File Management

HI

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??

-s-
LVL 1
socrossAsked:
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.
<?php
$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));
				fclose($fTempHandle);
				
				$fMainHandle = fopen($mainCSV, 'w') or die("Couldn't open main .csv file!");
				fwrite($fMainHandle, $tempString);
				fclose($fMainhandle);
			}
		}
		closedir($dh);
	}
}
 
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

0
 
m00003643Commented:
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.
0
 
firehazrdCommented:
Just use a database... mysql print out your data as a csv later...
0
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.

thanks

-s-
0
 
m00003643Commented:
* closedir($dh) should also be closedir($d);

0
 
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.

cyberwebservice
Experts Exchange Cleanup Volunteer
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
All Courses

From novice to tech pro — start learning today.