file_exists() causing problem?

Hi,

I m uploading a ~160KB text file with 3800 lines of stock data. The lines are read and submitted into my database.

I upload using this condition:

 move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)

Where $uploadfile = $uploaddir . basename($_FILES[‘userfile’][‘name’]);

It uploads the file and adds to the database fine. The execution takes almost 1 minute, and almost always draws a file exists error (if file_exists($uploadfile) ).

Is it because the time for file upload is too long?

What’s the actual code and the actual error message that it generates?

As follows, its strange because the file exists error is before the file upload attempt.


//Name of folder
$uploaddir = 'EOD/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

//No file loaded
if (!$_FILES['userfile']['tmp_name']) {
    echo "<b>Error:</b> ". FILE_UPLOAD_NO_FILE ."<br>";

//File size is zero    
} elseif ($_FILES['userfile']['size']==0) {
    echo "<b>Error:</b> ". FILE_UPLOAD_ZERO_SIZE ."<br>";

//All stock data will entered from text files
} elseif ($_FILES['userfile']['type'] != "text/plain") {
    echo "<b>Error:</b> ". FILE_UPLOAD_WRONG_TYPE ."<br>";

//Possible file attack, HTML uploaded file not equal to temp file found
} elseif (!is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    echo "<b>Error:</b> ". FILE_UPLOAD_FILE_ATTACK ."<br>";

//Found file / file already uploaded
} elseif (file_exists($uploadfile)) {
    echo "<b>Error:</b> ". FILE_UPLOAD_ALREADY_EXIST ."<br>";

} else {

    //Uploading process, read, and extract to data base
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {

    //Functions to open and exact data and insert into DB

    } else {
        echo "<b>Error:</b> ". FILE_UPLOAD_FAIL ."<br>";                        
    }
}

In your op you said

and almost always draws a file exists error (if file_exists($uploadfile) ).

Post the error mesaage that gets displayed on your screen?

My apologies, maybe I wasn’t being clear.

The error message (as shown in the code) triggers my error message, which is somewhat arbitrary.

The issue is that it uploads fine, inserts values into the database fine, THEN calls a file_exist() error.

Ive switched methods, to FTP uploading the files, and the issue still occurs. I don’t understand why a previous if condition is being trigger :S

What you are saying here is that after the file has been uploaded by the code you posted, values are then stored into a database and then the error is triggered. So the loading into the database and the error being triggered happens below the code you posted and so I have no idea what you have done in that code because you haven’t posted it.

Under what conditions would a previous statement be called? Unless it was looped (it is not). The contents of the function doesn’t matter in this case, I can’t imagine a condition where the contents revisits the unless it includes the page (from which it is initially called from!)

The issue turned out to be caused by a race condition from when a file_exist() function and the move_uploaded_file() function is called. file_exist() is, from my understanding, time intensive. I tried to use clearstatcache() to no avail either (but wasn’t sure on where it’s application would have been best placed). Curiously, even applying an adequate sleep condition didn’t work.

I couldn’t find much documentation on the subject (this primarily: http://php.bigresource.com/Track/php-yH0StQF2/) so I might work up and example and have it looked at.