7

Why does is_dir() return false even if it is a dir?

returns no error

$path_mysql = '/var/lib/mysql/';
if(!is_dir($path_mysql)){
    echo 'error';
}

returns error

$path_mysql = '/var/lib/mysql/domain.com/';
if(!is_dir($path_mysql)){
    echo 'error';
}

/var/lib/mysql/domain.com/ does exsits, but is_dir() returns false!?

I can get access to the dir through both PuTTY and WinSCP

6
  • have you checked permissions?
    – gustavotkg
    Nov 10, 2011 at 15:08
  • The directories inside /var/lib/mysql are typically only readable by the mysql user. Apache & PHP won't be able to access them. Nov 10, 2011 at 15:09
  • If you are creating domain.com/ on the fly then you might need to do clearstatcache() before the check.
    – Jon
    Nov 10, 2011 at 15:09
  • If filename is a relative filename, it will be checked relative to the current working directory. If you have enabled safe mode, or open_basedir further restrictions may apply. see docu [php.net/manual/en/function.is-dir.php]
    – Irishka
    Nov 10, 2011 at 15:12
  • Do you have permission to read that folder?
    – gen_Eric
    Nov 10, 2011 at 15:14

3 Answers 3

5

Possibly because it cannot check whether the /var/lib/mysql/domain.com/ exists or not, because it does not have enough rights to do that (permission problems).

Check the execute (list) permission of /var/lib/mysql/ directory for the user which runs this PHP script (probably it's www-data)

3
  • How can you gain access to the dir? I need to backup the binary files
    – clarkk
    Nov 10, 2011 at 15:10
  • 1
    I'm not sure it's a good idea. Why don't you use mysqldump for backuping the MySQL database? Nov 10, 2011 at 15:11
  • can mysqldump be used to copy the binary data files?
    – clarkk
    Nov 10, 2011 at 15:12
0

Probably permissions - the MySQL data dirs are owned by the MySQL user. If you're running this test script under a webserver, it's highly unlikely that the webserver's UID has the rights to read anything in that directory, so is_dir returns false.

0

This is a pretty old question but I bumped into this issue of the failing is_dir() function with Ubuntu 22.04 and 23.10 recently.

So I hope this helps anyone with a simple solution.

FACT: The is_dir() function in PHP will fail (i.e., return false) if the parent directory to is not executable.

What I wanted to do...

I had my PHP 8.1 CodeIgniter project in var/www on the Ubuntu 22.04 OS. I wanted index.php to point to the rest of my project in the Home folder.

What was happening...

Since my home folder is not executable by default, the is_dir() function kept returning false.

Solution...

Simply make the home directory executable by adding the executable attribute to it.

cd /home
sudo chmod +x khalid

Basically you're going to the home folder in root, then adding the executable attribute to the folder identified by your user name. In my case it's khalid. This is what is shown as "Home" when browsing through nautilus.

I hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.