Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php is_file always return false

Tags:

php

php is_file always return false

[apache@h185 default]$ ls -l /home/www/default/p.php
-rwxr-xr-x. 1 zhouhh zhouhh 50837 Aug 28 19:02 /home/www/default/p.php
[apache@h185 default]$ ls -l /usr/bin/rrdtool
-rwxr-xr-x. 1 root root 24688 Aug 21  2010 /usr/bin/rrdtool
[apache@h185 default]$ ls -l /root/my.cnf
ls: cannot access /root/my.cnf: Permission denied
[apache@h185 default]$ ls -l /usr/bin/ld
-rwxr-xr-x. 1 root root 594968 Jun 22 22:06 /usr/bin/ld
[apache@h185 default]$ ls -l /usr/bin/php
-rwxr-xr-x. 1 root root 3224944 Jul  4 00:57 /usr/bin/php
[apache@h185 default]$ vi test.php
[apache@h185 default]$ cat test.php
<?php
#if(is_file('/home/www/default/p.php'))

#if(is_file('/usr/bin/rrdtool'))
#if(is_file('/root/my.cnf'))
#if(is_file('/usr/bin/ld'))
#if(file_exists('/usr/bin/ld'))
if(is_file('/usr/bin/php'))
{
print 'ok';
}
else
{
print 'no ok';
}
?>
[apache@h185 default]$

except first line returns true, other line always returns false. but this file all exist. /root/my.cnf can't access, other files can execute and read.

how to solve this problem?

like image 873
user1095131 Avatar asked Aug 29 '12 08:08

user1095131


3 Answers

Note that is_file() returns false if the parent directory doesn't have +x (running permissions) set for the user running the php file.

This make sense, but other functions such as readdir() don't seem to have this limitation. The end result is that you can loop through a directory's files but is_file() will always fail.

Quoted from here

like image 167
Kuf Avatar answered Oct 16 '22 22:10

Kuf


Note that www-data or web server user has to have the rights for accesing the directory whereas the file is, login as webserver user (su www-data in linux) and check you can access through all the path that you are using to access the file.

like image 35
Mariano Argañaraz Avatar answered Oct 16 '22 20:10

Mariano Argañaraz


You can also use !is_dir which doesn't suffer from running permissions issue.

if(!is_dir($filename)) {
  //remove a file
  unlink($filename);
} else {
  //remove a directory
}
like image 33
Joel Davey Avatar answered Oct 16 '22 22:10

Joel Davey