Вопрос

I'm not sure what's wrong here. For some reason is_dir function not working properly for me.

class A
{
  private $path

  public function setPath($path)
  {
     if(!is_dir(!$path)){
      throw new Exception('Invalid path');
    }

  }
}

$obj = new A;
$obj->setPath('/usr/htdocs/site/');

After this line of code the exception Invalid path is thrown. I checked and the /usr/htdocs/site/ path exists. What could be the problem?

Это было полезно?

Решение 2

!$path means if anything in !$path, it will be false. So it will be !is_dir(false).

I think your code will be if (!is_dir($path)) { ... NOT if (!is_dir(!$path)) {

Little Update:

As @NiettheDarkAbsol said, is_dir takes the string (the directory name) as parameter, so the false [which get from !$path ($path should be '/usr/htdocs/site/' in this case, so !$path should be bool false)] will be convert in to string '' (empty).

And the '' is a invalid directory name in most common used file systems. So, the is_dir will can't found the any directory by that invalid name. And because of that, it will return false, which triggered the exception throwing to report you the path is invalid.

Thanks to @NiettheDarkAbsol tell me the rest thing.

Другие советы

remove ! before path variable

try like this:

 if(!is_dir($path)){
      throw new Exception('Invalid path');
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top