为什么 file_exists() 不起作用?
这是我的 PHP 代码:
[root@file htdocs]# vi test.php
<?php
var_dump(file_exists('/usr/local/apache2/resumes/28/"Chapel Hill"/franky_li/"CV.doc"'));
?>
"test.php" [New] 5L, 100C written
[root@file htdocs]# php test.php
bool(false)
它说该文件不存在,但实际上它确实存在:
[root@file htdocs]# ls -l /usr/local/apache2/resumes/28/"Chapel Hill"/franky_li/"CV.doc"
-rw-r--r-- 1 daemon root 36864 Oct 17 2008 /usr/local/apache2/resumes/28/Chapel Hill/franky_li/CV.doc
[root@file htdocs]#
似乎确实是引用问题:
<?php
var_dump(file_exists('/usr/local/apache2/resumes/28/Chapel Hill/franky_li/CV.doc'));
?>
~
~
"test.php" 5L, 96C written
[root@file htdocs]# php test.php
bool(true)
[root@file htdocs]#
现在通过使用以下转换器修复:
preg_replace('/\/([^\/\s]+\s+[^\/]+)(?:\/|$)/','/"${1}"/',$file);
使其在 bash 中工作!
Here is my PHP code:
[root@file htdocs]# vi test.php
<?php
var_dump(file_exists('/usr/local/apache2/resumes/28/"Chapel Hill"/franky_li/"CV.doc"'));
?>
"test.php" [New] 5L, 100C written
[root@file htdocs]# php test.php
bool(false)
which says the file doesn't exists,but in fact it does:
[root@file htdocs]# ls -l /usr/local/apache2/resumes/28/"Chapel Hill"/franky_li/"CV.doc"
-rw-r--r-- 1 daemon root 36864 Oct 17 2008 /usr/local/apache2/resumes/28/Chapel Hill/franky_li/CV.doc
[root@file htdocs]#
seems it's indeed quote issue:
<?php
var_dump(file_exists('/usr/local/apache2/resumes/28/Chapel Hill/franky_li/CV.doc'));
?>
~
~
"test.php" 5L, 96C written
[root@file htdocs]# php test.php
bool(true)
[root@file htdocs]#
fixed now by using the following converter:
preg_replace('/\/([^\/\s]+\s+[^\/]+)(?:\/|$)/','/"${1}"/',$file);
to make it work in bash!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试删除双引号,因为它已经用单引号引起来了。
Try removing the double-quotes, since it's already quoted with single-quotes.
检查 file_exists 的手册。
请注意本节:
“对于由于安全模式限制而无法访问的文件,此函数返回 FALSE。但是,如果这些文件位于 safe_mode_include_dir 中,则仍然可以包含它们。”
我的猜测是您使用的是
Check the manual for file_exists.
Note this section:
"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."
My guess is that you're using <PHP 6.0.0, and you have safe_mode on (it is by default, and is on most hosts). If this is the case, you won't find the file unless it's included in safe_mode_include_dir.