PHP 文件存在总是错误
我遇到了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我刚刚经历过这个问题,并且在过去的几个小时里一直被困在这个问题上。答案是您需要指定 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.
应该是文件权限问题。确保您正在测试的文件可由
_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.
来自
file_exists()
这只是一个猜测,代码示例可能会让事情变得更清楚。
file_exists()
可能无法访问该文件的另一个原因(与安全模式无关):这个脚本在我的 Linux 机器上运行良好(这几乎是您添加的示例):
From the php manual on
file_exists()
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):This script works fine on my linux box (it's pretty much the example you added):
还要检查父目录及其所有父目录,以确保每个人都具有执行访问权限。
如果您在 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.这可能会有所帮助:
http://bugs.php.net/bug.php?id=48535
具体来说:
This may be of help:
http://bugs.php.net/bug.php?id=48535
Specifically:
使用
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.安全模式?
来自 PHP file_exists 文档:
Safe mode?
From PHP file_exists documentation: