Set_include_path () does not work with relative require ()

I “require” a file that has its own call to require (). But calling set_include_path () does not work with the relative path in require (). Unfortunately, I do not control the required code file.

C: /myapp/index.php - My code

set_include_path(get_include_path() . ';C:/app/subfolder');
require('C:/app/subfolder/index.php');

C: /app/subfolder/index.php - third-party code (no control)

require('../config.php'); // Require file located at C:/app/config.php

Result:

Failed to open window "../config.php" (include_path = '; C: / app / subfolder')

This does not work even if the C: / app / subfolder is in the include path. I would expect .. /config.php to be C: / app / subfolder /../ config.php. Maybe I'm doing it wrong.

In this case, C: /app/subfolder/index.php was not originally intended to be included in another PHP file. However, I am building the code in front of it, and I want C: /app/subfolder/index.php to still be able to require / include files as if it had never left its location.

I read that using dirname () or __DIR__ fixes the relative path problem in require () , but I cannot change the code in C: /app/subfolder/index.php .

Is there any way to make this work?

+4
source share
2 answers

Your problem is not area related set_include_path(). Your fatal error shows the actual include_pathone in which you can see your added path.

, ../ include_path, . , script HTTP-URL, , script .

script /usr/angelina/myproject/subfolder/index.php ( : Unix filePath/commands):

set_include_path( '/usr/angelina/myproject/subfolder' );
require( '../config.php' );

:

cd /usr/angelina/myproject/subfolder
php index.php

script . :

cd /usr/angelina/myproject
php subfolder/index.php

script , , script, .

, , - include, . , chdir().

, C:/myapp/index.php :

chdir( 'C:/app/subfolder' );
require( 'C:/app/subfolder/index.php' );

script (, : ...)

+3

PHP eval(), file_get_contents() str_replace(), :

<?php
$index = file_get_contents("C:/app/subfolder/index.php");

if (substr($index, 0, 5) == "<?php") $index = trim(substr($index, 5));

elseif (substr($index, 0, 2) == "<?") $index = trim(substr($index, 2));

if (substr($index, -2) == "?>") $index = trim(substr($index, 0, -2));

$index = str_replace("require('../config.php');", "require('C:/app/config.php');", $index);

eval($index);
?>

?

eval PHP , , . , str_replace() , eval() "C:/app/subfolder/index.php" PHP, , . PHP , , , .

0

All Articles