PHP directory problems

2019-09-04 15:01发布

问题:

I got a problem with this:

I got file xxx.php in which I'm parsing one file: $config['etc'] = parse_ini_file('config/config.txt');

Everything is fine, until I'm including xxx.php file to my yyy.php file using require_once("lang.inc.php");

Then I get an message: Warning: parse_ini_file(config/config.txt): failed to open stream: No such file or directory in ...

When I change ;

$config['etc'] = parse_ini_file('config/config.txt');

to

$config['etc'] = parse_ini_file('../config/config.txt');

then my yyy.php works fine, but xxx.php not... and I'm in stuck.

Help me guys, please.

回答1:

Try using absolute file paths if your PHP scripts can be invoked (or requested) from different directories. For example, the current directory of a PHP script executed under example.com/one.php will be different from example.com/folder/two.php.

Your change to

$config['etc'] = parse_ini_file('../config/config.txt');

is still relative, except this time it's just "one directory up from the current directory > config > config.txt", rather than before, where you were looking in the same current directory.

A generic fix would be to do the following:

$root = $_SERVER['DOCUMENT_ROOT'];
// ... code ...
$config['etc'] = parse_ini_file($root . '/config/config.txt');

There are possible issues with using the DOCUMENT_ROOT field, but it should be fine for the most part (such as DOCUMENT_ROOT not being defined under the CLI). Also, check if DOCUMENT_ROOT has a trailing slash, even though the standard is not to have it, some hosts may have the trailing slash included. If it does, remove your slash at the beginning of your file path.



回答2:

You should use absolute path or path from yyy.php to config.txt
TO get absolute one you can use $_SERVER['DOCUMENT_ROOT'] or __FILE__ constant



回答3:

Maybe it's because your file hierarchy looks like this?

/
- file/
-- config/
---config.txt
--- yyy.php
- xxx.php

xxx.php and yyy.php is not in the same directory. When you include xxx.php in yyy.php, the parse_ini_file will read the file from the yyy.php perspective. That's why you need to change the directory, because when you include a file, all of its direcotry reading changed.

The solution is of course has been written by others (use absolute path), or you can just move the files to the same direcotry. I'm just writes this for the full explanation.

EDIT:

Of course, I'm just speculating your file hierarchy. Just try to imagine the file hierarchy, and see if xxx.php and yyy.php is not in the same directory. That might be the problem.

EDIT 2: I've tried the situation you write in the question, and it works without problem. This is the code I made:

xxx.php:

<?php
    $config = parse_ini_file('config/config.txt');

yyy.php:

<?php
    require_once "xxx.php";

The file hierarchy:

/
- config/
-- config.txt
- xxx.php
- yyy.php

Works just fine. Maybe your yyy.php is included on another file, and called from that file?



回答4:

try this:

class Database extends PDO{ public $koneksi;   function
__construct($file = './../db/config.ini'){
    try{

   if (!$settings = parse_ini_file(__DIR__.$file, TRUE)) throw new
exception('Unable to open ' . $file . '.');
        $driver=$settings['database']['driver'];
        $dsn=$settings['database']['dsn'];
        $port=$settings['database']['port'];
        $dbname=$settings['database']['dbname'];        $username=$settings['database']['username'];
   $password=$settings['database']['password'];

        $this->koneksi = new PDO("mysql:host=" .$dsn . ";dbname=" . $dbname, $username,$password,array(PDO::ATTR_ERRMODE =>
PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES
utf8"));
        $this->koneksi->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    }catch(PDOException $e){
        $e->getMessage();
    }   
}


标签: php directory