0

opendir() gives me a PHP warning if a folder cannot be accessed.

I can use is_dir but what if the folder gets deleted in the meantime:

  if(is_dir($some_dir)){
    // HERE, DELETED, GONE :(
    $d = opendir($some_dir);
    // warning !!0
  }

that warning messes up my entire error handling code.

Is there any way to do file manipulation without getting warnings? FALSE results are enough to understand that the file is inaccessible, I don't understand why does it have to warn me too :(

Please dont tell me to use @. There has to be a better way

thelolcat
  • 10,995
  • 21
  • 60
  • 102
  • Would this be something you could use? http://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning – Kai Sep 18 '14 at 19:44
  • can you try using try and catch? – codelogn Sep 18 '14 at 19:44
  • The documentation on [opendir()](http://php.net/manual/en/function.opendir.php) explicitly mentions the @ solution. However, it seems like you need to synchronize different processes which could be achieved by [flock()](http://php.net/manual/en/function.flock.php). – andy Sep 18 '14 at 19:52

3 Answers3

1
$d = @opendir($some_dir);

@ before a function silence errors the function could raise.

Then you can test $d to see if it worked or not (False if not working).

Oh right, thanks comments, OP mentionned not to tell about @ my bad.

Then another possibility is to change your php configuration so php doesn't raise warnings.

Loïc
  • 11,804
  • 1
  • 31
  • 49
0

Try the following by checking if that variable is set in the first place.

if(isset($some_dir)):
  if(is_dir($some_dir)):
    // HERE, DELETED, GONE :(
    $d = opendir($some_dir);
    // warning !!0
  endif;
endif;
Mubo
  • 1,078
  • 8
  • 16
0

Try this code

  if(is_dir($some_dir)){
    // HERE, DELETED, GONE
    $d = @opendir($some_dir);
    // warning
  }

If you don't want to use '@' then you have to check/confirm your directory path on $some_dir is correct

Seegan See
  • 101
  • 1
  • 1
  • 9