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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

m00003643Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.