PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
dull1554
Forum Regular
Posts: 680 Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W
Post
by dull1554 » Thu Jun 03, 2004 6:36 pm
Code: Select all
<?php
$base = "c:/folder/";
function getDirList($base)
{
$int = 0;
if(is_dir($base)){
$dh = opendir($base);
while (false !== ($dir = readdir($dh))) {
if (is_dir($base . $dir) && $dir !== '.' && $dir !== '..') {
$subs = $dir;
$subbase = $base . $dir . '/';
print $subbase . "
";
$array[$int] = $subbase;
$int++;
getDirList($subbase);
}
}
closedir($dh);
}
return $array;
}
getDirList($base);
print_r($array);
?>
this is a function slighlty modified from the php manual usernotes, it prints out the directories but if i try to assign the values to an array and then return the array, it telles me that it does not exist
even though when i run it it prints out 4 dirs
Code: Select all
c:/folder/adf/
c:/folder/adf/New Folder/
c:/folder/adf/New Folder/asdfa/
c:/folder/asdasdfsad/
and these valuse should also be assigned to the array but they arent
any thoughts?
p.s. if i get this to work it will be a soultion to an earilier question/ or a means to an end rather
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Jun 03, 2004 6:41 pm
getDirList($base); should be $array = getDirList($base); otherwise the returned $array 'gets lost'.
I'd also initialise $array in the function...
$int = 0;
$array = array();
dull1554
Forum Regular
Posts: 680 Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W
Post
by dull1554 » Thu Jun 03, 2004 6:47 pm
now when i run it i get this error
Notice: C:\Program Files\Apache Group\Apache\htdocs\indexbuilder.php line 25 - Undefined variable: array
new code:
Code: Select all
<?php
$base = "c:/folder/";
function getDirList($base)
{
$int = 0;
$array = array();
if(is_dir($base)){
$dh = opendir($base);
while (false !== ($dir = readdir($dh))) {
if (is_dir($base . $dir) && $dir !== '.' && $dir !== '..') {
$subs = $dir;
$subbase = $base . $dir . '/';
print $subbase . "
";
$array[$int] = $subbase;
$int++;
$array = getDirList($subbase);
}
}
closedir($dh);
}
return $array;
}
getDirList($base);
print_r($array);
?>
any thoughts?
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Jun 03, 2004 6:48 pm
getDirList($base); should be $array = getDirList($base); otherwise the returned $array 'gets lost'.
Sorry i meant to do that when you call the function outside the function, not where you iteratively call it inside the function.
Code: Select all
<?php
$base = "c:/folder/";
function getDirList($base)
{
$int = 0;
$array = array();
if(is_dir($base)){
$dh = opendir($base);
while (false !== ($dir = readdir($dh))) {
if (is_dir($base . $dir) && $dir !== '.' && $dir !== '..') {
$subs = $dir;
$subbase = $base . $dir . '/';
print $subbase . "
";
$array[$int] = $subbase;
$int++;
getDirList($subbase);
}
}
closedir($dh);
}
return $array;
}
$array = getDirList($base);
print_r($array);
?>
dull1554
Forum Regular
Posts: 680 Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W
Post
by dull1554 » Thu Jun 03, 2004 7:08 pm
oh...wow...a little dense
dull1554
Forum Regular
Posts: 680 Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W
Post
by dull1554 » Thu Jun 03, 2004 7:10 pm
now it works but it wont work inside the function
Code: Select all
<?php
$base = "c:/folder/";
function getDirList($base)
{
$int = 0;
$array = array();
if(is_dir($base)){
$dh = opendir($base);
while (false !== ($dir = readdir($dh))) {
if (is_dir($base . $dir) && $dir !== '.' && $dir !== '..') {
$subs = $dir;
$subbase = $base . $dir . '/';
print $subbase . "
";
$array[$int] = $subbase;
$int++;
getDirList($subbase);//this is not working or not running at all
}
}
closedir($dh);
}
return $array;
}
$array = getDirList($base);
print_r($array);
?>
it returns the base directories inside the directory specified
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Jun 03, 2004 7:29 pm
Try this version.
Code: Select all
<?php
$base = "c:/folder/";
function getDirList($base, $array = array())
{
if(is_dir($base)){
$dh = opendir($base);
while (false !== ($dir = readdir($dh))) {
if (is_dir($base . $dir) && $dir !== '.' && $dir !== '..') {
$subbase = $base . $dir . '/';
echo $subbase.'<br />';
$array[] = $subbase;
$array = getDirList($subbase, $array);
}
}
closedir($dh);
}
return $array;
}
$array = getDirList($base);
print_r($array);
?>
Last edited by
markl999 on Thu Jun 03, 2004 7:33 pm, edited 1 time in total.
dull1554
Forum Regular
Posts: 680 Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W
Post
by dull1554 » Thu Jun 03, 2004 7:33 pm
that works perfectly
thanks a bunch
dull1554
Forum Regular
Posts: 680 Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W
Post
by dull1554 » Thu Jun 03, 2004 8:29 pm
ok ive gotten quite a ways now
heres what i have, when i try to use in_array()
it tells me that im using the wrong datatype, but i know im using it right, a string for the needle, and an array for the array
Code: Select all
<?php
$base = "c:/folder/";
function getDirList($base, $array = array())
{
if(is_dir($base)){
$dh = opendir($base);
while (false !== ($dir = readdir($dh))) {
if (is_dir($base . $dir) && $dir !== '.' && $dir !== '..') {
$subbase = $base . $dir . '/';
$array[] = $subbase;
$array = getDirList($subbase, $array);
}
}
closedir($dh);
}
if(!empty($array)){
return $array;
} else {
return array();
}
}
$array = getDirList($base);
function getDirFiles($dirPath)
{
if ($handle = opendir($dirPath))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$filesArr[] = trim($file);
}
}
closedir($handle);
}
return $filesArr;
}
function parsearray($array)
{
$num = count($array);
for($i = 0; $i <= $num; $i++)
{
$filearray = getDirFiles($array[$i]);
print_r($filearray);
if(in_array("index.php", $filearray))
{
}
else
{
$fp = fopen("index.php", 'a+');
$fc = fclose($fp);
}
}
}
parsearray($array);
?>
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Jun 03, 2004 8:33 pm
for($i = 0; $i <= $num; $i++)
should be
for($i = 0; $i <= $num-1; $i++)
(or use a foreach instead)
[edit] or for($i = 0; $i < $num; $i++)
dull1554
Forum Regular
Posts: 680 Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W
Post
by dull1554 » Thu Jun 03, 2004 8:42 pm
i still get
Warning: in_array(): Wrong datatype for second argument in c:\program files\apache group\apache\htdocs\indexbuilder.php on line 45
and i have never really used a foreach statement, ive been looking at the manual but it does not really explain it too well