1

I am trying to read a file from a directory 2 folders lower than the script. The code works fine when the file is in the same directory but when its lower, it fails every time. Here's My code

<?php
$logfile = '/pass/uploads/test.aes';
$my_file = file_get_contents("$logfile");
echo $my_file;
?>

Ideas?

mikemmb73
  • 75
  • 2
  • 3
  • 13

3 Answers3

3

A filename beginning with / is an absolute name, and is relative to the root of the filesystem.

Remove the beginning slash if it's meant to be a name relative to the current directory. Or, tack __DIR__ onto the beginning of the name, like __DIR__ . '/pass/uploads/test.aes'.

cHao
  • 84,970
  • 20
  • 145
  • 172
2

Try this code

<?php
    $logfile = '../../pass/uploads/test.aes';
    $my_file = file_get_contents($logfile);
    echo $my_file;
?>
Ry-
  • 218,210
  • 55
  • 464
  • 476
admoghal
  • 1,498
  • 1
  • 10
  • 5
1

You should just be able to use ../../ at the beginning of the path to go up two directories, unless the permissions on those directories are set incorrectly.

EDIT: as cHao said, adding / to the path will go to the root of the file system, which will almost certainly result in a permission error.

Tortoise
  • 208
  • 3
  • 11