PHP 文件存在总是错误

发布于 2024-08-12 06:45:02 字数 461 浏览 8 评论 0原文

我遇到了 file_exists() 总是返回 false 的情况。我最近的尝试是测试 $_SERVER["SCRIPT_FILENAME"] 是否返回 true,然后如果找不到它所找到的文件,则返回路径的值。

虽然与解决问题不一定相关的路径是: /Users/joe/Workspace/720/app/webroot/index.php

我显然已经验证了该文件是否存在,但我什至不确定它怎么可能不在那里因为 php 正在提供它。

我应该提到的是,这是在运行 PHP 5.3.0 的 OS X Snow Leopard 上安装的。

任何想法都会很棒。

代码示例:

if (!file_exists($_SERVER["SCRIPT_FILENAME"]))
    $errors[] = 'Cant find:'. $_SERVER["SCRIPT_FILENAME"];

I have a case where file_exists() is always returning false. My latest attempt was to just test to see if it would return true for $_SERVER["SCRIPT_FILENAME"] and then return the value of the path if it couldn't find the file which it does.

The path while not necessarily relevant to solving the problem is: /Users/joe/Workspace/720/app/webroot/index.php

I have obviously verified that the file is there, and am not even sure how it couldn't be there since php is serving it up.

I should mention this is on an install of OS X Snow Leopard running PHP 5.3.0.

Any ideas would be fantastic.

CODE SAMPLE:

if (!file_exists($_SERVER["SCRIPT_FILENAME"]))
    $errors[] = 'Cant find:'. $_SERVER["SCRIPT_FILENAME"];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

软的没边 2024-08-19 06:45:02

我刚刚经历过这个问题,并且在过去的几个小时里一直被困在这个问题上。答案是您需要指定 ABSOULTE 路径才能使 file_exists() 正常工作。您不能使用相对路径,例如“dir1/images/image.jpg”或“../../images/image1.jpg”。您需要指定“/rootdir/subdir/dir1/images/myimage.jpg”。

无论如何,这对我有用。

I just experienced this issue and have been stuck on it for the past couple hours. The answer is that you need to specify the ABSOULTE path in order for file_exists() to work. You can NOT use relative paths such as 'dir1/images/image.jpg' or '../../images/image1.jpg'. You need to specify '/rootdir/subdir/dir1/images/myimage.jpg'.

That's what worked for me anyway.

∞觅青森が 2024-08-19 06:45:02

应该是文件权限问题。确保您正在测试的文件可由 _www 用户(即用于在 Mac OS X 上运行 apache {httpd} 的用户)访问。

也许您可以尝试使用 777 作为权限位来测试 /tmp 上的文件。

希望有帮助。

It's probably a file permission issue. Make sure the file you are testing for is accessible by _www user (which is the user used to run apache {httpd} on Mac OS X).

Maybe you can try testing for a file on /tmp with 777 as it permission bits.

Hope it helps.

贪恋 2024-08-19 06:45:02

来自 file_exists()

对于由于安全模式限制而无法访问的文件,此函数返回 FALSE。但是,如果这些文件位于 safe_mode_include_dir 中,则仍然可以包含它们。

这只是一个猜测,代码示例可能会让事情变得更清楚。

file_exists() 可能无法访问该文件的另一个原因(与安全模式无关):

注意:检查是使用真实的UID/GID而不是有效的UID/GID来完成的。

这个脚本在我的 Linux 机器上运行良好(这几乎是您添加的示例):

<?php
  if (file_exists($_SERVER["SCRIPT_FILENAME"])){
    echo "Found File: ";
  } else {
    echo "No File: ";
  }

  echo $_SERVER["SCRIPT_FILENAME"];
?>

From the php manual on file_exists()

This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir.

That's only a guess, a code sample may make things clearer.

Another reason file_exists() may not be able to access the file (not safe mode related):

Note: The check is done using the real UID/GID instead of the effective one.

This script works fine on my linux box (it's pretty much the example you added):

<?php
  if (file_exists($_SERVER["SCRIPT_FILENAME"])){
    echo "Found File: ";
  } else {
    echo "No File: ";
  }

  echo $_SERVER["SCRIPT_FILENAME"];
?>
归属感 2024-08-19 06:45:02

还要检查父目录及其所有父目录,以确保每个人都具有执行访问权限。

如果您在 Apache 下运行(而不是在命令行上),请记住它在 Snow Leopard 上的 _www 用户和 _www 组下运行。这就是需要访问权限的组。

Also check the parent directory, and all of their parents, to make sure that everyone has execute access.

If you're running this under Apache (instead of on the command line), remember that it runs under the _www user and _www group on Snow Leopard. So that's the group that needs access.

萌能量女王 2024-08-19 06:45:02

这可能会有所帮助:
http://bugs.php.net/bug.php?id=48535

具体来说:

我已将 php.ini 中的 fastcgi.impersonate 设置为 1(例如
文档中推荐)。
如果我将其设置为 0,它就可以工作。

This may be of help:
http://bugs.php.net/bug.php?id=48535

Specifically:

I've set the setting fastcgi.impersonate in php.ini to 1 (like
recommendet in the documentation).
If I set it to 0 it works.

奈何桥上唱咆哮 2024-08-19 06:45:02

使用 file_exists($_SERVER['DOCUMENT_ROOT']."/path/to/file") 这样 php 就会为您提供绝对路径。

Use file_exists($_SERVER['DOCUMENT_ROOT']."/path/to/file") this way php will give it the absolute path for you.

星星的軌跡 2024-08-19 06:45:02

安全模式?

来自 PHP file_exists 文档

警告

该函数对于文件返回 FALSE
由于安全模式无法访问
限制。然而这些文件
如果是的话,仍然可以包括在内
位于 safe_mode_include_dir。

Safe mode?

From PHP file_exists documentation:

Warning

This function returns FALSE for files
inaccessible due to safe mode
restrictions. However these files
still can be included if they are
located in safe_mode_include_dir.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文