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 9 of 9
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    file_exists() trouble

    i am having difficulty with the file_exists() function. After lots of debugging its seems to be the reason why my program creates a file duplicate.

    I use the user's name as the filename and have to check if there is already a file with the name that the program is about to create, if there is then I attach a random integer to the name and create that file instead. when i do so, however, two files are created as opposed to one. it's really weird. I ran the page without the checking utility and only one file is created. also it seems to happen only because i have the file_exists() in a while loop. when i put into a if statement only one file is created. my code:

    Code:
    while(file_exists($file_dir)==true)	{
    	$filename = $filename.rand(0,9);
    	$file_dir = 'user_files/'.$filename.'.txt';
    }
    $file = fopen($file_dir,"w");
    //write stuff to file
    fclose($file);
    what is going on.
    I should add that the duplicate file created is actually caries a different name.(the old name with a random int attached) the content is the same.
    thanks for the help.

  • #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
    file_exists() is not creating any files... there must be more going on that what your code snippet shows, because you're right, that makes no sense.

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts
    i have figured out that if i take out the portion of the code that appends the random integer then there is no duplicate created it just overwrites the existing file. When teh file name is appended with the random int there are two created one with the last random integer appended and an additional one with yeat another random integer created. as if the page is coming out of a recursive call and executes twice....

  • #4
    New Coder
    Join Date
    Jul 2008
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Another bit that I have figured out is that it is not only the appending of a random int that does this. The code below also creates the duplicate situation:

    Code:
    $filename = "tomasz";
    $file_dir = "user_files/".$filename.".txt";
    
    if(file_exists($file_dir))
    	$file_dir = "user_files/".$filename."a.txt";
    
    
    $file = fopen($file_dir,"w");
    
    
    fclose($file);
    This code creates two files, tomasz.txt and tomasza.txt
    how is this possible???????

    i am testing this on an apache server.

  • #5
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    A possible explanation:

    The first time your script is run the file "tomasz.txt" does not exist and so your script creates the file. When the script is executed a second time "tomasz.txt" does exist and so your script decides to create "tomasza.txt" instead.

    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #6
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    Description
    string tempnam ( string $dir , string $prefix )

    Creates a file with a unique filename, with access permission set to 0600, in the specified directory. If the directory does not exist, tempnam() may generate a file in the system's temporary directory, and return the name of that.
    http://uk3.php.net/manual/en/function.tempnam.php

    Use the username as your prefix, and it should work. Looping with random numbers has the potential to create all kinds of problems. Google: 'race condition'
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #7
    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
    I don't see tempnam() being used anywhere.

    I still say the reason for all this is outside the scope of where you are looking and showing... such as your script is being run twice, or it's in a loop you're not expecting, or something like that. Put in a die("test") right after the file is closed and then check to see which files got created.

  • #8
    New Coder
    Join Date
    Jul 2008
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts
    when i place die() right after the fclose, both files are generated tomasz.txt and tomasza.txt in on run of the script even thought beforehand i erase everything from the directory that is being written in.

  • #9
    New Coder
    Join Date
    Jul 2008
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts
    well, i got to work, but i have no idea how????

    i simply made a test.php file and wrote a small script to do the same on a much smaller scale as my original file. The code that did not work on the original file worked fine the first time. Then I began importing code from the original until the site stopped working again and began creating duplicates. This was sadly the html header on top????????? I have no idea how this is possible. The site worked perfectly with the original header generated by dreamweaver, but would not operate with the original header plus html color and style. ridiculus...


  •  

    Posting Permissions

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