为什么file_exists()不起作用?

时间:2009-06-04 01:00:30

标签: php quotes 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中运行!

2 个答案:

答案 0 :(得分:3)

请尝试删除双引号,因为它已经引用了单引号。

答案 1 :(得分:0)

查看file_exists的手册。

请注意本节:

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

我的猜测是你正在使用&lt; PHP 6.0.0,并且你有safe_mode(它是默认情况下,并且在大多数主机上)。如果是这种情况,除非它包含在safe_mode_include_dir中,否则您将找不到该文件。