首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么php找不到这个文件?

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

Stack Overflow用户
提问于 2018-08-20 03:44:31
回答 1查看 66关注 0票数 0

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

utilities.php在/合唱团/公司

hitCounter.txt在/合唱团/etc/tbc

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

代码语言:javascript
复制
$hits = file_get_contents('../etc/tbc/hitCounter.txt');

它会产生此错误:

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

这是我第一次使用PHP,我不知道为什么它找不到文件。我试过单引号和双引号,但都没有用。

我知道有人会要求获得完整的代码,下面是utilities.php文件:

代码语言:javascript
复制
<?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;
}
?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-20 05:07:29

1)应该明确您的文件路径。在这种情况下很难说。我们应该有我们的根应用程序文件夹。

如果我们遵循MVC模式,我们将很容易获得根应用程序文件夹。

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

我喜欢这样的东西:

代码语言:javascript
复制
$file = APP_ROOT . '/etc/tbc/hitCounter.txt';
#APP_ROOT  has the path /mnt/DroboFS/Shares/DroboApps/apache/www/Choir

2)检查file_exists

代码语言:javascript
复制
if (!file_exists($file)) {
    //Throw error here
}

3)检查:is_readable

代码语言:javascript
复制
if (!is_readable($File)) {
   ......
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51923899

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档