473,473 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Including all the files in a directory

4 New Member
I write a function named import() to import all files under a folder:
[PHP]define('DS', DIRECTORY_SEPARATOR);
define('ROOTDIR', realpath(dirname(__FILE__).DS.'..'.DS));
function import($dir)
{
$dirObj = dir($dir);
if ($dirObj->handle !== false)
{
while (false !== ($entry = $dirObj->read()))
{
if ($entry == '.' || $entry == '..')
{
continue;
}

{
$rtn = include_once($dir.DS.$entry);
print $rtn;
}
}
}
else
{
die('no such directory');
}
$dirObj->close();
}
[/PHP]
Then I put this function into gobal.php which is included in index.php
[PHP]require_once('includes/global.php');
import( ROOTDIR.'config' );
print $db_host;[/PHP]

Under the config folder I hava a file named config.php :
[PHP]$db_host = "localhost";
$db_user = "root";
$db_pwd = "123456";
$db_name = "match";[/PHP]

The problem is it doesn't print "localhost" but nothing.
If i put the code of import() function in index.php direct ,it work successfully.
why? Thank you very much .
Oct 6 '07 #1
6 2011
pbmods
5,821 Recognized Expert Expert
Heya, ModelKing. Welcome to TSDN!

In this line:
Expand|Select|Wrap|Line Numbers
  1. define('ROOTDIR', realpath(dirname(__FILE__).DS.'..'.DS));
  2.  
__FILE__ is the path of global.php, not index.php.
Oct 6 '07 #2
modelking
4 New Member
Maybe you just mistake what I mean. You can test this code on your machine.ROOTDIR is the path of index.php. And i just care about why my function could not work like what i want. Thank you for your reply.
Oct 7 '07 #3
phizzle
3 New Member
modelking,
I believe your problem is actually a scoping issue. Because you make your include inside of a function, all of those variables that you included are scoped into only that function. If you want proof of this, place
Expand|Select|Wrap|Line Numbers
  1. global $db_host
at the top of your import function.
I'd hate to disappoint you, but I can't think of a solution to this right now. This best I can think right now is:
in global.php:
Expand|Select|Wrap|Line Numbers
  1.       function import($dir)
  2.       {
  3.       $files_to_import = array();
  4.       $dirObj = dir($dir);
  5.       if ($dirObj->handle !== false)
  6.       {
  7.         while (false !== ($entry = $dirObj->read()))
  8.         {
  9.          if ($entry == '.' || $entry == '..')
  10.          {
  11.           continue;
  12.          }
  13.          {
  14.           $files_to_import[] = $dir.DS.$entry;
  15.          }
  16.         }
  17.       }
  18.       else
  19.       {
  20.         die('no such directory');
  21.       }
  22.       $dirObj->close();
  23.       return $files_to_import;
  24.       }
  25.  
and in index.php
Expand|Select|Wrap|Line Numbers
  1.       require_once('include/global.php');
  2.       $files = import( ROOTDIR.'/config' );
  3.       foreach($files as $file) {
  4.           include_once($file);
  5.       }
  6.       echo $db_host;
  7.  
But I don't think this is what you are looking for.

Also, I would move your ROOTDIR definition into the index.php, and make it:
Expand|Select|Wrap|Line Numbers
  1. define('ROOTDIR', realpath(dirname(__FILE__)));
That way your global is even more global ;).
Oct 7 '07 #4
modelking
4 New Member
dear phizzle ,thanks very much for your reply and advice.
I have got what you mean, but I still have problem with this code:
[PHP]
require_once('includes/global.php');
import( ROOTDIR.'config' );
print_r (get_included_files());
print $GLOBALS['db_host'];

function import($dirname)
{
$dirObj = dir($dirname);
$include = array();
if ($dirObj->handle !== false)
{
while (false !== ($entry = $dirObj->read()))
{
if ($entry == '.' || $entry == '..')
{
continue;
}
array_push($include, $dirObj->path.DS.$entry);
}
foreach ($include as $file)
{
$rtn = include_once($file);
print $rtn;
}
}
else
{
die('no such directory');
}
$dirObj->close();
}
[/PHP]

If it is a scoping problem, why the above code print out the include files? It puzzle me 。
Oct 7 '07 #5
modelking
4 New Member
dear phizzle ,I have got your means. Thanks you very much . I just go a wrong way.
Oct 7 '07 #6
pbmods
5,821 Recognized Expert Expert
Heya, modelking.

My point was that ROOTDIR was pointing to the wrong directory because it was looking for files relative to global.php instead of index.php.

__FILE__ resolves to the path of the file that *contains* __FILE__, not the path of the script that is being executed.

Were you able to get it working?
Oct 7 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: lawrence | last post by:
2 Questions: 1.) Can anyone think of a way to speed up this function? It is terribly slow. I plan to reduce the number of directories to 3, which I guess will speed it up in the end. 2.) This...
3
by: Peter Taurins | last post by:
Hi there. I have an included file (header.php) that contanis a reference to a graphic. If I stay at the root level, then I can control the relative path of the image. eg. images/imagename.jpg ...
4
by: WindAndWaves | last post by:
Hi Gurus I hope I am going to make sense with this question: I have an html page that I have turned into a php page with a bit of php code above the html (connect to database, massage data a...
3
by: Sean Quinn | last post by:
Hi, I don't know if anyone has run into similar problems, but it seems like when I use `require_once(...)' with files that contain functions I get an error indicating that it can't redeclare the...
2
by: David | last post by:
I'm using following code for checking a file existence. I's working fine for given folder. Is there a way to check a file exitance in subfolders? Thanks in advance, David Option Compare...
1
by: jj | last post by:
Hi folks: I can get the list of files in a certain directory by using Directory.GetFiles("c:\temp"). But I want to get the number of files (ex. 20) within a directory including all...
31
by: Joseph Wakeling | last post by:
Hello all, I'm writing some programs that will be using modules from the GNU Scientific Library (GSL). Include commands within the GSL modules have commands such as, #include...
3
by: Steve Teeples | last post by:
I have a simple application built with several DLLs that access XML files for information. My local setup works just fine. As defined in my Install folder, the files are copied to the...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
3
by: KIRAN | last post by:
Hello all, My question is about the way of including header files(*.h) in source files (*.c) I have three folders, -build ( for project makefiles) -include ( for *.h files) -src (for *.c...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.