Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2004
    Posts
    128
    Thanks
    6
    Thanked 0 Times in 0 Posts

    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!

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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

  • Users who have thanked Fumigator for this post:

    XtremeGamer99 (12-02-2010)

  • #3
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    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

  • Users who have thanked Lamped for this post:

    XtremeGamer99 (12-02-2010)

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Posts
    128
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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 07:23 PM.

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    yeah that is way better. I've never used tmpfile() before and it is the perfect thing for what you're doing.

  • #6
    Regular Coder
    Join Date
    Jun 2004
    Posts
    128
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Use chmod() to make MySQL happy?
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •