Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-02-2010, 12:24 AM   PM User | #1
XtremeGamer99
Regular Coder

 
Join Date: Jun 2004
Posts: 128
Thanks: 6
Thanked 0 Times in 0 Posts
XtremeGamer99 is an unknown quantity at this point
File handling question...

I have an application that writes to a file, and uses that file to load a MySQL table. Here's the current code:

PHP 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!
XtremeGamer99 is offline   Reply With Quote
Old 12-02-2010, 02:56 AM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
You can add flock() to your process for an added bit of security, but according to many of the comments left in the PHP manual, it is not 100% foolproof. Read the comments for more information and for some ideas on what other people have done to make sure files don't become corrupted due to multiple simultaneous accesses.

http://us2.php.net/manual/en/function.flock.php
__________________
Fumigator is offline   Reply With Quote
Users who have thanked Fumigator for this post:
XtremeGamer99 (12-02-2010)
Old 12-02-2010, 08:17 AM   PM User | #3
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
tempnam() would be perfect for your needs. It guarantees a unique filename, and you can override it to use a folder of your choice.

http://php.net/manual/en/function.tempnam.php
__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Users who have thanked Lamped for this post:
XtremeGamer99 (12-02-2010)
Old 12-02-2010, 06:21 PM   PM User | #4
XtremeGamer99
Regular Coder

 
Join Date: Jun 2004
Posts: 128
Thanks: 6
Thanked 0 Times in 0 Posts
XtremeGamer99 is an unknown quantity at this point
Thank you both!

tmpfile() seems to be slightly better since it's removed when closed, which is one less thing I have to do. =D

Last edited by XtremeGamer99; 12-02-2010 at 06:23 PM..
XtremeGamer99 is offline   Reply With Quote
Old 12-02-2010, 09:17 PM   PM User | #5
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
yeah that is way better. I've never used tmpfile() before and it is the perfect thing for what you're doing.
__________________
Fumigator is offline   Reply With Quote
Old 12-04-2010, 08:19 AM   PM User | #6
XtremeGamer99
Regular Coder

 
Join Date: Jun 2004
Posts: 128
Thanks: 6
Thanked 0 Times in 0 Posts
XtremeGamer99 is an unknown quantity at this point
Update: Tried it, turns out the permissions don't set right for MySQL to read the file via LOAD DATA INFILE.

=/ Oh well. I'll keep searching, but in the meantime I'm using md_rand().microtime(true) for a hopefully unique filename.
XtremeGamer99 is offline   Reply With Quote
Old 12-04-2010, 11:15 AM   PM User | #7
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
Use chmod() to make MySQL happy?
__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:19 PM.


Advertisement
Log in to turn off these ads.