problem with file_exists caching even when clearstatcache() is called

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Crowe

    problem with file_exists caching even when clearstatcache() is called

    Hi guys,

    I've created this little function to check whether a user has uploaded a
    file with the same name as an existing file, and if so rename it to
    file-1.jpg, file-2.jpg etc.

    clearstatcache( );
    if(file_exists( "$BasePath/$FileName.jpg") ){
    $extra=1;
    while(file_exis ts("$BasePath/$FileName-$extra.jpg")){
    $extra++;
    }
    $FileName="$Fil eName-$extra";
    }

    However if files are deleted outside PHP file_exists still returns true so
    it keeps adding higher numbers to the files.


    Is there any way I can get round this? The strange thing is if I call
    file_exists() on a folder it works fine.

    --
    Regards,
    Andrew Crowe


  • Andrew Crowe

    #2
    Re: problem with file_exists caching even when clearstatcache( ) is called

    Sorry forgot to mention I'm using PHP5 RC2 on Windows 2000 / IIS (isapi)

    --
    Regards,
    Andrew Crowe


    Comment

    • Chung Leong

      #3
      Re: problem with file_exists caching even when clearstatcache( ) is called

      "Andrew Crowe" <andrewcrowe_uk @yahoo.co.uk> wrote in message
      news:40752e40$0 $3940$afc38c87@ news.easynet.co .uk...[color=blue]
      > Hi guys,
      >
      > I've created this little function to check whether a user has uploaded a
      > file with the same name as an existing file, and if so rename it to
      > file-1.jpg, file-2.jpg etc.
      >
      > clearstatcache( );
      > if(file_exists( "$BasePath/$FileName.jpg") ){
      > $extra=1;
      > while(file_exis ts("$BasePath/$FileName-$extra.jpg")){
      > $extra++;
      > }
      > $FileName="$Fil eName-$extra";
      > }
      >
      > However if files are deleted outside PHP file_exists still returns true so
      > it keeps adding higher numbers to the files.
      >
      >
      > Is there any way I can get round this? The strange thing is if I call
      > file_exists() on a folder it works fine.
      >
      > --
      > Regards,
      > Andrew Crowe
      >[/color]

      There's a race condition in your code. Between the time when file_exists()
      was called and the subsequent move_uploaded_f ile() a file with that name
      could conceivably have been created. A better approach is to keep calling
      move_uploaded_f ile() until it works:

      $extra = 1;
      while(!@move_up load_file($tmpn ame, $filename)) {
      $filename = "$BasePath/$FileName-$extra.jpg";
      $extra++;
      }

      This bypasses the caching issue as well.


      Comment

      Working...