PHP dynamic require -
the main issue it's still same:
this proyect
/ | index.php |-test1 | test.php
the test.php
<?php echo $variable; ?>
the index.php
<?php $variable = "<br>index"; echo 'test<br>'; $full_path = dirname(__file__)."/test1"; $adm_extension = array( "php"); if (is_dir($full_path)) { if (($handle = opendir($full_path))) { while ($file = readdir($handle)) { if (is_file($full_path . '/' . $file)) { $item_path = $full_path . '/' . $file; $extension = pathinfo($item_path, pathinfo_extension); if (in_array($extension, $adm_extension)) { require $item_path; } } } } }
and it's works charm, output is:
test index
if want encapsulate functionality in function this:
index1.php
$variable = "<br>index"; echo 'test<br>'; $full_path = dirname(__file__)."/test1"; $adm_extension = array( "php" ); function rtest($full_path, $adm_extension){ if (is_dir($full_path)) { if (($handle = opendir($full_path))) { while ($file = readdir($handle)) { if (is_file($full_path . '/' . $file)) { $item_path = $full_path . '/' . $file; $extension = pathinfo($item_path, pathinfo_extension); if (in_array($extension, $adm_extension)) { require $item_path; } } } } } } rtest($full_path, $adm_extension);
i got this:
( ! ) notice: undefined variable: variable in c:\wamp\www\sandbox\test1\test.php on line 3
any clue??
your script probably, when going through files, opens test.php
before index1.php
$variable
isn't defined yet
test it:
function rtest($full_path, $adm_extension){ if (is_dir($full_path)) { if (($handle = opendir($full_path))) { while ($file = readdir($handle)) { if (is_file($full_path . '/' . $file)) { $item_path = $full_path . '/' . $file; $extension = pathinfo($item_path, pathinfo_extension); if (in_array($extension, $adm_extension)) { echo $item_path.'<br />'; require $item_path; } } } } } }
Comments
Post a Comment