PHP file_exists($ var)不起作用 [英] PHP file_exists($var) not working

查看:148
本文介绍了PHP file_exists($ var)不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的笔记本上写一些代码,并使用xampp环境。我有以下一段代码:

  class A {
...
foreach($ blocks as $ block){
$ block ='dir / dir2 /'。 $块;

if(file_exists($ block)== true){
$ var。= file_get_contents($ block);




$ b $ p $当我在foreach循环中回显$ block变量时,它会返回文件的路径。但是,file_exists函数总是返回false。你可以帮我弄清楚这里有什么问题吗?

解决方案

file_exists 目的是检查提供的文件是否存在。这是返回false。这意味着你的文件不存在于php正在寻找的地方。 PHP可能正在寻找一个比你期望的不同领域。看来是时候进行一些调试了。



运行这个来找出php正在查找的位置。

  echo当前工作目录是 - >。 GETCWD(); 

这就是你想要php的样子吗?如果没有,那么改变目录php正在使用 chdir 函数。

  $ searchdirectory =c:\path\to\your\directory; //如果需要,使用unix样式路径
chdir($ searchdirectory);然后运行你的函数(注意:为了与windows风格保持一致,我把斜杠翻转成了反斜杠)

  class A {
...
//改变工作目录
$ searchdirectory =c:\path\to\your\directory; //如果需要,使用unix样式路径
chdir($ searchdirectory);

foreach($ blocks as $ block){
$ block ='dir \dir2\'。 $块;
$ b $ if(file_exists($ block)== true){
$ var。= file_get_contents($ block);
}
}
}


I'm trying to write some code on my notebook and am using the xampp environment. I have the following piece of code:

class A {
...
  foreach ($blocks as $block) {
    $block = 'dir/dir2/' . $block;
  }
  if (file_exists($block) == true) {
    $var .= file_get_contents($block);
  }
}

When I echo the $block variable in the foreach loop, it gives back the path to the files. However, the file_exists function always returns false. Could you please help me figure out what's wrong here?

解决方案

file_exists purpose is to check if the supplied file exists. It's returning false. This means that the your file doesn't exist where php is looking. php might be looking in a different area than you expect. Looks like it's time for some debugging.

Run this to figure out where php is looking.

echo "current working directory is -> ". getcwd();

Is that where you want php to look? If not then change the directory php is looking in with the chdir function.

$searchdirectory = "c:\path\to\your\directory"; //use unix style paths if necessary
chdir($searchdirectory);

Then run your function (note: I flipped the slashes to backslashes in order to be consistent with windows style paths.)

class A {
...
  //change working directory
  $searchdirectory = "c:\path\to\your\directory"; //use unix style paths if necessary
  chdir($searchdirectory);

  foreach ($blocks as $block) {
    $block = 'dir\dir2\' . $block;

    if (file_exists($block) == true) {
      $var .= file_get_contents($block);
    }
  }
}

这篇关于PHP file_exists($ var)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆