Reputation: 81
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?
Upvotes: 5
Views: 16118
Reputation: 85
I know this is old, but in my case, I was getting my file names from a file, and splitting it or the newline. Adding trim() to remove hidden characters from the end of my filename fixed my issue. Apparently hidden symbols added to the filename aren't automatically parsed out.
Upvotes: 0
Reputation: 175
As a general rule check your file permissions in accordance with previous answers.
In a Docker build environment, files copied as a result of host:container directory mappings may not have copied correctly. If you see -??????????
for your file's permissions, manually copy the file(s) with docker cp
.
Upvotes: 0
Reputation: 15186
I had the same problem. The issue was an incorrect file path.
Always make sure that e.g. is_file($dir.$file)
is pointing to the correct location.
Output your recent location by: var_dump($dir.$file);
Output if the correct file path is set (must be true): var_dump(is_file($dir.$file));
Upvotes: 0
Reputation: 2603
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
}
Upvotes: 1
Reputation: 17828
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
Upvotes: 10
Reputation: 1252
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.
Upvotes: 2
Reputation: 2587
is_file()
returns if a file is regular file.
when you do a ls -l if the first column of the file permissions is a -, then that file is a regular file.
so use file_exists()
function. It returns if a file/directory exists.
Upvotes: 0