我有一个在 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/