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条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证