Jump to content

[SOLVED] Includes all files but 1


MasterACE14

Recommended Posts

with my modules script it is including all the files it should be, besides one.

one of them is adding a folder to the filepath, when it doesn't need that folder.

 

Here's the script...

class Modules {

public $dirname;

function __construct() {
	$this->dirname = GETPATH_MODULES;
}

// Select Modules
function selectModules($dirname=null) {

	if(!isset($dirname)) {
		$this->dirname = GETPATH_MODULES;
	} else {
		$this->dirname = $dirname;
	}
	$dir = opendir($this->dirname);
	$file_list = '';
   
	while(false !== ($file = readdir($dir))) {
		if(($file != ".") and ($file != "..")) {
			if(is_dir($this->dirname . '/' . $file)) {
				$file_list .= $this->selectModules($this->dirname . DS . $file);
			}
			if(substr($file,"-4") == ".php") {
				if($file != "loader.php") {
					$file_list .= $this->dirname.DS.$file.",";
					$file_list .= str_replace("/",DS,$file_list);
				}
			}
			else {
			 // all other file formats
			}
		}
	}
	closedir($dir);
	return $file_list;
}

// Load Modules
function loadModules() {
	$modules = $this->selectModules();
	$modules = explode(",",$modules);
	$modulesCount = (count($modules)-1);
	for($i=0;$i<$modulesCount;$i++)
	{
		require_once($modules[$i]);
	}
	return true;
}

}

$modules = new Modules();
$modules->selectModules();
$modules->loadModules();

 

output...

BFHello World Module says Hello!

Warning: require_once(C:\wamp\www\GETenginebeta\modules\helloworld\z.php) [function.require-once]: failed to open stream: No such file or directory in C:\wamp\www\GETenginebeta\modules\loader.php on line 59

 

Fatal error: require_once() [function.require]: Failed opening required 'C:\wamp\www\GETenginebeta\modules\helloworld\z.php' (include_path='.;C:\php5\pear') in C:\wamp\www\GETenginebeta\modules\loader.php on line 59

 

The modules directory is setup like this...

== modules

- loader.php

- b.php

- z.php

- f.php

 

= helloworld

-helloworld.php

 

b, z and f php files simply echo their respective letter, and helloworld.php echo's exactly that. But for some reason, the class is adding the helloworld folder to the filepath when it tries to include z.php

 

any help is greatly appreciated.

 

Regards, Ace

Link to comment
https://forums.phpfreaks.com/topic/162521-solved-includes-all-files-but-1/
Share on other sites

I suspect your recursive while loop isn't working as you suspect it is.

You're changing the internal $this->dirname variable when you do your recursive call, meaning that dirname is maintained in the calling loop. And I suspect because it's doing an ascending alphabetical read, it does b.php, f.php, helloworld (dir), helloworld/helloworld.php, (at this point dirname is helloworld), so it calls helloworld/z.php

I'm not having much luck with getting it to work right.

 

I know the problem is here...

if(is_dir($this->dirname . '/' . $file)) {
$file_list .= $this->selectModules($this->dirname . DS . $file);
}

 

but passing the directory as an argument isn't working right, and without it, it doesn't work at all :-\

I've changed bits and pieces of the code trying to fix the error.

 

I have managed to get rid of the error. But now the class is only including the helloworld/helloworld.php file, it's not including b.php , f.php or z.php in the modules directory.

 

my output is simply...

Hello World Module says Hello!

when it should be...

Hello World Module says Hello!BFZ

 

here's the latest code.

class Modules {

public $dirname;

// Select Modules
function selectModules($dirname=null) {

	$this->dirname = $dirname;
	$dir = opendir($this->dirname);

	if(!isset($file_list)) $file_list = '';
   
	while(false !== ($file = readdir($dir))) {
		if(($file != ".") and ($file != "..")) {
			if(is_dir($this->dirname . DS . $file)) {
				$file_list .= $this->selectModules($this->dirname . DS . $file);
			}
			elseif(substr($file,"-4") == ".php") {
				if($file != "loader.php") {
					$file_list .= $this->dirname.DS.$file.",";
					$file_list .= str_replace("/",DS,$file_list);
				}
			}
			else {
			 // all other file formats
			}
		}
	}
	closedir($dir);
	return $file_list;
}

// Load Modules
function loadModules() {
	$modules = $this->selectModules($this->dirname);
	$modules = explode(",",$modules);
	$modulesCount = (count($modules)-1);
	for($i=0;$i<$modulesCount;$i++)
	{
		require_once($modules[$i]);
	}
	return true;
}

}

// load modules
$modules = new Modules();
$modules->selectModules(GETPATH_MODULES);
$modules->loadModules();

I have...

class Modules {

public $dirname;

// Select Modules
function selectModules($dirname=null) {

	if($dirname == null) {
		$this->dirname = GETPATH_MODULES;
	} else {
		$this->dirname = $dirname;	
	}

	$dir = opendir($this->dirname);

	if(!isset($file_list)) $file_list = '';
   
	while(false !== ($file = readdir($dir))) {
		if(($file != ".") and ($file != "..")) {
			if(is_dir($this->dirname . DS . $file)) {
				$file_list .= $this->selectModules($this->dirname . DS . $file);
			}
			elseif(substr($file,"-4") == ".php") {
				if($file != "loader.php") {
					$file_list .= $this->dirname.DS.$file.",";
					$file_list .= str_replace("/",DS,$file_list);
				}
			}
			else {
			 // all other file formats
			}
		}
	}
	closedir($dir);
	return $file_list;
}

// Load Modules
function loadModules() {
	$modules = $this->selectModules();
	$modules = explode(",",$modules);
	$modulesCount = (count($modules)-1);
	for($i=0;$i<$modulesCount;$i++)
	{
		echo $i." - ".$modules[$i]."<br />";
		//require_once($modules[$i]);
	}
	return true;
}

}

// load modules
$modules = new Modules();
$modules->selectModules();
$modules->loadModules();

 

It appears to be looping through all the files that it is suppose to include, twice:

0 - C:\wamp\www\GETenginebeta\modules\b.php

1 - C:\wamp\www\GETenginebeta\modules\b.php

2 - C:\wamp\www\GETenginebeta\modules\f.php

3 - C:\wamp\www\GETenginebeta\modules\b.php

4 - C:\wamp\www\GETenginebeta\modules\b.php

5 - C:\wamp\www\GETenginebeta\modules\f.php

6 - C:\wamp\www\GETenginebeta\modules\helloworld\helloworld.php

7 - C:\wamp\www\GETenginebeta\modules\helloworld\helloworld.php

8 - C:\wamp\www\GETenginebeta\modules\helloworld\z.php

9 - C:\wamp\www\GETenginebeta\modules\b.php

10 - C:\wamp\www\GETenginebeta\modules\b.php

11 - C:\wamp\www\GETenginebeta\modules\f.php

12 - C:\wamp\www\GETenginebeta\modules\b.php

13 - C:\wamp\www\GETenginebeta\modules\b.php

14 - C:\wamp\www\GETenginebeta\modules\f.php

15 - C:\wamp\www\GETenginebeta\modules\helloworld\helloworld.php

16 - C:\wamp\www\GETenginebeta\modules\helloworld\helloworld.php

17 - C:\wamp\www\GETenginebeta\modules\helloworld\z.php

 

But why?  :-\

 

and is still trying to include z.php in the helloworld folder, when it is in the modules folder like the rest of the letters.

 

 

commenting out...

//	$file_list .= str_replace("/",DS,$file_list);

 

returns...

0 - C:\wamp\www\GETenginebeta\modules\b.php

1 - C:\wamp\www\GETenginebeta\modules\f.php

2 - C:\wamp\www\GETenginebeta\modules\helloworld\helloworld.php

3 - C:\wamp\www\GETenginebeta\modules\helloworld\z.php

 

which is correct besides it trying to get z.php from inside helloworld still. Rather then just in modules.

 

if I change my selectModules() method to the following...

	// Select Modules
function selectModules($dirname=null) {

	if($dirname == null) {
		$this->dirname = GETPATH_MODULES;
	} else {
		$this->dirname = $dirname;	
	}

	$dir = opendir($this->dirname);

	if(!isset($file_list)) $file_list = '';
   
	while(false !== ($file = readdir($dir))) {
		if(($file != ".") and ($file != "..")) {
			if(is_file($this->dirname.DS.$file)) {
				if(substr($file,"-4") == ".php") {
				if($file != "loader.php") {
					$file_list .= $this->dirname.DS.$file.",";
				}
			}
			else {
			 // all other file formats
			 $file_list .= "Hello,";
			}
			}
			elseif(is_dir($this->dirname . DS . $file)) {
				$file_list .= $this->selectModules($this->dirname . DS . $file);
			}
		}
	}
	closedir($dir);
	return $file_list;
}

it doesn't find z.php at all...

0 - C:\wamp\www\GETenginebeta\modules\b.php

1 - C:\wamp\www\GETenginebeta\modules\f.php

2 - C:\wamp\www\GETenginebeta\modules\helloworld\helloworld.php

however that is working 100% , it's just not including the z.php module like it should.

 

any ideas?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.