richbai90
richbai90

Reputation: 5204

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!";
}

Upvotes: 1

Views: 10454

Answers (1)

Bram
Bram

Reputation: 4532

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.

Upvotes: 5

Related Questions