XtremeGamer99
12-02-2010, 12:24 AM
I have an application that writes to a file, and uses that file to load a MySQL table. Here's the current code:
if (!empty($this->goesIntoDatabase)) {
$time->add("Start sql file writing");
$sqlfile = fopen('/tmp/sqlFile.sql', "w");
foreach ($this->goesIntoDatabase as $id => $text) {
$data = explode('|', $id);
fwrite ($sqlfile, $data[0]."\t".$data[1]."\t".$text."\n");
}
fclose($sqlfile);
$time->add("End sql file writing");
$time->add("Start process()->data insertion");
$this->DB->e("
LOAD DATA INFILE '/tmp/sqlFile.sql'
INTO TABLE `data` (@time, `author`, `text`)
SET
`time` = UNIX_TIMESTAMP(@time),
`channelID` = ?",
$channelInternalID);
unlink('/tmp/sqlFile.sql');
$time->add("End process()->data insertion");
}
I'm worried that while one person is utilizing the script, another person starts it. And since the script uses the same file location, I'm worried that using the script two or more times at the same time will result in a conflict. Is this possible?
I already plan to append the filename with a semi-unique number, maybe PHP's session ID or microtime(), or both for safe measure, so that each run of the script has it's own file. But this post is more of a clarification on the way files work in PHP.
Thanks!
if (!empty($this->goesIntoDatabase)) {
$time->add("Start sql file writing");
$sqlfile = fopen('/tmp/sqlFile.sql', "w");
foreach ($this->goesIntoDatabase as $id => $text) {
$data = explode('|', $id);
fwrite ($sqlfile, $data[0]."\t".$data[1]."\t".$text."\n");
}
fclose($sqlfile);
$time->add("End sql file writing");
$time->add("Start process()->data insertion");
$this->DB->e("
LOAD DATA INFILE '/tmp/sqlFile.sql'
INTO TABLE `data` (@time, `author`, `text`)
SET
`time` = UNIX_TIMESTAMP(@time),
`channelID` = ?",
$channelInternalID);
unlink('/tmp/sqlFile.sql');
$time->add("End process()->data insertion");
}
I'm worried that while one person is utilizing the script, another person starts it. And since the script uses the same file location, I'm worried that using the script two or more times at the same time will result in a conflict. Is this possible?
I already plan to append the filename with a semi-unique number, maybe PHP's session ID or microtime(), or both for safe measure, so that each run of the script has it's own file. But this post is more of a clarification on the way files work in PHP.
Thanks!