0

I have an Apache/PHP site running on a Drobo5n in Linux.

utilities.php is in /Choir/inc

hitCounter.txt is in /Choir/etc/tbc

In utilities.php, we have the following line of code:

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

Which produces this error:

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

This is my first time fiddling with PHP and I cannot figure out why it can't find the file. I've tried both single and double quotes around the path to no avail.

I know someones gonna ask for the complete code so here's the utilities.php file:

<?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;
}
?>
tolsen64
  • 881
  • 1
  • 9
  • 22
  • 2
    You should provide a fullpath to start with, e.g. `file_get_contents(__DIR__ . '/../etc/...`, but you'll also need to make sure that your Apache user has permission to read that file – scrowler Aug 20 '18 at 03:47

1 Answers1

1

1) Should explicit your file path. Hard to say in this case. We should have our root application folder.
If we follow the MVC pattern, we will get the root application folder easily.

For example https://github.com/daveh/php-mvc

I like something:

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

2) Check file_exists

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

3) Check: is_readable

if (!is_readable($File)) {
   ......
}
Khoa TruongDinh
  • 913
  • 1
  • 13
  • 26
  • 1
    PHP must have changed since 2011? The site used to run as-is until it was taken offline. Now I've been asked to get it up & running again. Back then the double-dot to back up a directory worked. But now it doesn't work unless I provide the full path. Thanks to you and Robbie above for pointing this out. – tolsen64 Aug 21 '18 at 04:07