View Full Version : file_exists() trouble
08-10-2008, 05:38 AM
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:
$filename = $filename.rand(0,9);
$file_dir = 'user_files/'.$filename.'.txt';
$file = fopen($file_dir,"w");
//write stuff to 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.
08-10-2008, 06:55 AM
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.
08-10-2008, 06:59 AM
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....
08-10-2008, 07:27 AM
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:
$filename = "tomasz";
$file_dir = "user_files/".$filename.".txt";
$file_dir = "user_files/".$filename."a.txt";
$file = fopen($file_dir,"w");
This code creates two files, tomasz.txt and tomasza.txt
how is this possible???????
i am testing this on an apache server.
08-10-2008, 10:41 AM
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.
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.
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'
08-10-2008, 05:47 PM
I don't see tempnam() being used anywhere. :confused:
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.
08-10-2008, 06:13 PM
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.
08-10-2008, 10:00 PM
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...
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.