2009-06-04 48 views
1

这是我的PHP代码:为什么file_exists()不工作?

[[email protected] 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 
[[email protected] htdocs]# php test.php 
bool(false) 

它说文件不存在,但实际上它的作用:

[[email protected] 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 
[[email protected] htdocs]# 

似乎是确实的报价问题:

<?php 


var_dump(file_exists('/usr/local/apache2/resumes/28/Chapel Hill/franky_li/CV.doc')); 
?> 
~ 
~ 
"test.php" 5L, 96C written 
[[email protected] htdocs]# php test.php 
bool(true) 
[[email protected] htdocs]# 

现在通过使用以下转换器来修复:

preg_replace('/\/([^\/\s]+\s+[^\/]+)(?:\/|$)/','/"${1}"/',$file); 

使它在bash中工作!

+0

[PHP的file_exists()的可能的重复将不适用于我?](http://stackoverflow.com/questions/1287837/phps-file-exists-will-not-work-for-me) – razlebe 2011-10-28 07:51:40

回答

3

尝试删除双引号,因为它已经用单引号括起来。

+0

我需要运行一个bash命令的exec(),任何一方都不能以这种方式工作。 – omg 2009-06-04 01:06:57

+0

在不同位置的不同文件上尝试file_exists()以查看它是否有效。如果它确实有效,那么你的问题是无法访问。如果它不起作用,那么问题就出在命令上,我们可以从那里开始工作。让我知道。 – 2009-06-04 01:13:13

0

查看file_exists的使用说明书。

注意本节:“此功能由于安全模式的限制返回FALSE的文件无法访问然而,这些文件还可以包含,如果它们位于safe_mode_include_dir”

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