php - 为什么php找不到这个文件?

标签 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');

产生此错误的原因:

Warning: file_get_contents(../etc/tbc/hitCounter.txt): failed to open stream: No such file or directory in /mnt/DroboFS/Shares/DroboApps/apache/www/Choir/inc/utilities.php on line 6

这是我第一次摆弄 PHP,我不明白为什么找不到文件。我在路径周围尝试了单引号和双引号都无济于事。

我知道有人会要求完整的代码,所以这是 utilities.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) 应该明确你的文件路径。在这种情况下很难说。我们应该有我们的根应用程序文件夹。
如果我们遵循 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)) {
   ......
}

关于php - 为什么php找不到这个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51923899/

相关文章:

linux - 到地址的逻辑 block 号(Linux 文件系统)

java - 无法使用 maven-archetype-webapp 插件创建基于 maven 的应用程序

php - Symfony2 @UniqueEntity 对 2 个外键的约束不起作用

javascript - 允许用户单击 select2 并保留自由文本

linux - "linux process address space"是如何存储的?

php - 无法使用 PHP 5.5 在 Ubuntu 14.04 上的 Laravel 4.2 中获得任何路由

php - 从 php 运行 csf 命令而不是 root

php - OpenVBX (Twilio) 中的 SQL 存储

php - 将 View 子文件夹路由到 zend 中的 Controller

C Linux printf 损坏的结果