dongque3797 2016-05-02 20:43
浏览 51
已采纳

无法使用file_exists查找文件

I am trying to look for a file called test.txt within the folder called .test.

<?php 
 $path = '/localhost/joshcms/.test/test.txt';
 $fileexists = file_exists($path);
 if ($fileexists == '1') {
  $result = 'true';
 } else {
   $result = 'false';
 }
?>

Please excuse the horrible formatting, I am creating this PHP within Jade.

I, first of all, had the $path variable set to .test/test.txt so when it searched for the file, it looked in: localhost/joshcms/.test/test.txt because it was a relative path; because that never worked, I then changed the code to look the way it is above and yet, it still does not work. I then tried with a file that was in the same folder and that worked fine.

I'm not too sure if this error is occurring due to me trying to find something in a hidden folder or if it's just the function doesn't work unless the file is in the same directory as the PHP script/file itself.

  • 写回答

2条回答 默认 最新

  • dongye9820 2016-05-02 20:49
    关注

    file_exists() returns bool(true|false), so checking the response against a string '1' like so if ($fileexists == '1') will not pass.

    The following will work:

    $path = '/localhost/joshcms/.test/test.txt';
    if ( file_exists($path) ) { // returns bool(true|false) response
        $result = 'true'; // if file exists, this condition will satisfy as the file does exist bool(true)
    } else {
       $result = 'false'; // if the file at $path does not exist, this will satisfy; bool(false)
    }
    

    Aside: $result will now, per your code, hold a string value of true/false. If you want $result to hold a boolean value of true/false, remove the single-quotes, ie. $result = true; and $result = false;, respectively.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:功能监听网页
  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群