简体   繁体   中英

file_get_contents no such file or directory php

I'm trying to build a php class that I can use to access my database safely, and keep my credentials away from prying eyes. As part of the constructor I'm building an array of my credentials from a file outside of the web directory. Unfortunately though I've tested the path using other functions in php and using vim and putty, I keep getting the same no such file or directory error when I try.

the line that causes the error looks like this

$this->creds = explode("\n",file_get_contents("../../../../../private/mysqlinfo.ini"));

the folder structure on my server lookls like this

~/public_html/mysubdomain/projectDirectory/php/includes

the file I 'm trying to access would be here

~/private/mysqlinfo.ini

The following function from the same directory shows me that the directory is accurate and that the mysqlinfo.ini file does exist in the directory

$dir = "../../../../../private";

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while(($file = readdir($dh)) !== false) {
            echo "filename:  $file || filetype: ". filetype($dir . $file) . "\n";
        }
    } else {
        echo "could not open directory";
    }
} else {
    echo "not a directory!";
}

The problem can probably be solved by using absolute paths instead of relative paths. You can always try to print the full path to the dir of the file by using echo dirname(__FILE__); in a PHP script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM