php为什么找不到此文件?

时间:2018-08-20 03:44:32

标签: php linux apache drobo

我在Linux的Drobo5n上运行一个Apache / PHP站点。

utilities.php位于/ Choir / inc

hitCounter.txt位于/ Choir / etc / tbc

在utilities.php中,我们具有以下代码行:

$hits = file_get_contents('../etc/tbc/hitCounter.txt');

哪个会产生此错误:

  

警告:file_get_contents(../ etc / tbc / hitCounter.txt):打开失败   流:中没有这样的文件或目录   /mnt/DroboFS/Shares/DroboApps/apache/www/Choir/inc/utilities.php   在第6行

这是我第一次摆弄PHP,我不知道为什么找不到文件。我在路径上都尝试了单引号和双引号都没有用。

我知道有人会要求完整的代码,所以这是Utility.php文件:

<?php
session_cache_limiter('private_no_expire');
session_start();

function getHitCount() {
    $hits = file_get_contents('../etc/tbc/hitCounter.txt');
    if (!isset ($_SESSION['beenHere'])) {
    $hits = $hits + 1;
    file_put_contents('../etc/tbc/hitCounter.txt', "$hits");
    $_SESSION['beenHere'] = "Yes I have";
    }
   return $hits;
}
?>

1 个答案:

答案 0 :(得分:1)

1)应该明确您的文件路径。在这种情况下很难说。我们应该有我们的根应用程序文件夹。
如果我们遵循MVC模式,则将轻松获得根应用程序文件夹。

例如https://github.com/daveh/php-mvc

我喜欢:

$file = APP_ROOT . '/etc/tbc/hitCounter.txt';
#APP_ROOT  has the path /mnt/DroboFS/Shares/DroboApps/apache/www/Choir

2)选中file_exists

if (!file_exists($file)) {
    //Throw error here
}

3)检查:is_readable

if (!is_readable($File)) {
   ......
}