简体   繁体   English

php is_file 总是返回 false

[英]php is_file always return false

php is_file always return false php is_file 总是返回 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. /root/my.cnf 无法访问,其他文件可以执行和读取。

how to solve this problem?如何解决这个问题呢?

Note that is_file() returns false if the parent directory doesn't have +x (running permissions) set for the user running the php file.请注意,如果父目录没有为运行 php 文件的用户设置+x (运行权限),则is_file()返回false

This make sense, but other functions such as readdir() don't seem to have this limitation.这是有道理的,但其他函数如readdir()似乎没有这个限制。 The end result is that you can loop through a directory's files but is_file() will always fail.最终结果是您可以遍历目录的文件,但is_file()将始终失败。

Quoted from here这里引用

请注意,www-data 或 web 服务器用户必须具有访问目录的权限,而文件是,以 webserver 用户身份登录(linux 中的 su www-data)并检查您是否可以通过您用来访问的所有路径进行访问文件。

You can also use !is_dir which doesn't suffer from running permissions issue.您还可以使用 !is_dir ,它不会受到运行权限问题的影响。

if(!is_dir($filename)) {
  //remove a file
  unlink($filename);
} else {
  //remove a directory
}

is_file() returns if a file is regular file . is_file()如果文件是常规文件则返回。

when you do a ls -l if the first column of the file permissions is a -, then that file is a regular file.当您执行 ls -l 时,如果文件权限的第一列是 -,则该文件是常规文件。

so use file_exists() function.所以使用file_exists()函数。 It returns if a file/directory exists.如果文件/目录存在,则返回。

I had the same problem.我有同样的问题。 The issue was an incorrect file path.问题是文件路径不正确。

Always make sure that eg is_file($dir.$file) is pointing to the correct location.始终确保例如is_file($dir.$file)指向正确的位置。

Output your recent location by: var_dump($dir.$file);通过以下方式输出您最近的位置: var_dump($dir.$file);

Output if the correct file path is set (must be true): var_dump(is_file($dir.$file));如果设置了正确的文件路径,则输出(必须为真): var_dump(is_file($dir.$file));

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.在 Docker 构建环境中,由于主机:容器目录映射而复制的文件可能未正确复制。 If you see -??????????如果你看到-?????????? for your file's permissions, manually copy the file(s) with docker cp .对于您的文件权限,请使用docker cp手动复制文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM