Reading a file in a different directory php

前端 未结 3 1943
心在旅途
心在旅途 2021-01-27 04:32

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.

相关标签:
3条回答
  • 2021-01-27 05:20

    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.

    0 讨论(0)
  • 2021-01-27 05:30

    Try this code

    <?php
        $logfile = '../../pass/uploads/test.aes';
        $my_file = file_get_contents($logfile);
        echo $my_file;
    ?>
    
    0 讨论(0)
  • 2021-01-27 05:37

    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'.

    0 讨论(0)
提交回复
热议问题