Welcome! Log In Create A New Profile

Advanced

[PHP] I am stumped

Posted by Jeffry Killen 
Jeffry Killen
[PHP] I am stumped
April 29, 2018 06:20AM
Hello;
I have the following code snips from isolated development project:
I have been doing lots of coding similar to this and don't ever remember encountering
this type of problem. I have a working version based on this elsewhere, as well. I saved
this message as a draft while I looked over the working code and accept for variable name
differences and differences in foreach looping construct: file or dir is tested in the looping
construct and not in core gerDirListSingle code.

This is supposed to scan a directory and produce an associative array where the index is the
file listing and the value is either directory or file.

In the loop, $_itr is set to 1 instead of 0 to try and solve the eventual output question, but doesn't
do it in tests.

private function getDirListSingle($_dir)
{
$_DR = [];
if(($_DR = scandir($_dir)) === false)
{
self::$_readError[count(self::$_readError)] = $_dir." not scanned";
return;
}
for($_itr = 1; $_itr < count($_DR); $_itr++)
{
switch($_DR[$_itr])
{
case '.':
case '..':
break;
default:
if(is_dir(self::$_homePath.'/'.$_DR[$_itr]))
{
self::$_dirsToRead[$_DR[$_itr]] = 'directory';
}
else
{
self::$_dirsToRead[$_DR[$_itr]] = 'file';
}
break;
}
}
}

next is code to produce a string representation of the array:
($_list['dirs'] is a copy of self::$_dirsToRead)

foreach($_list['dirs'] as $_ind=>$_val)
{
$_outList[count($_outList)] = $_val.": ".$_ind;
}
$_out = implode($_outList, "\n");

The following is the result:
testBed is the directory being read for this
Why is testBed/test_1 included and the value is 1
instead of 'directory'. At the bottom of the list is test_1 listed as a file.
Perhaps I have gone blind and can't see a mistake I should be seeing.

// $_out printed out:
1: testBed/test_1
file: hello.txt
file: hello_1.txt
file: index.php
file: test_1


Thanks for time and attention;
JK
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph M. Becker
[PHP] Re: I am stumped
April 29, 2018 11:50AM
On 29.04.2018 at 06:16, Jeffry Killen wrote:

> if(($_DR = scandir($_dir)) === false)
[…]
> for($_itr = 1; $_itr < count($_DR); $_itr++)
>
> testBed is the directory being read for this
> Why is testBed/test_1 included and the value is 1
> instead of 'directory'. At the bottom of the list is test_1 listed as a file.

An array created by scandir() is indexed from 0 to count()-1. The first
two elements normally have the values '.' and '..', respectively.
You're starting from 1, so '.' is skipped, but '..' produces the test_1
in your case.

--
Christoph M. Becker

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jeffry Killen
Re: [PHP] Re: I am stumped
April 29, 2018 06:40PM
> On Apr 29, 2018, at 2:39 AM, Christoph M. Becker <[email protected]> wrote:
>
> On 29.04.2018 at 06:16, Jeffry Killen wrote:
>
>> if(($_DR = scandir($_dir)) === false)
> […]
>> for($_itr = 1; $_itr < count($_DR); $_itr++)
>>
>> testBed is the directory being read for this
>> Why is testBed/test_1 included and the value is 1
>> instead of 'directory'. At the bottom of the list is test_1 listed as a file.
>
> An array created by scandir() is indexed from 0 to count()-1. The first
> two elements normally have the values '.' and '..', respectively.
> You're starting from 1, so '.' is skipped, but '..' produces the test_1
> in your case.
>

thanks for reply;

O.K. I understand that but the switch block should be skipping . and ..
anything that is not . or .. is handled in the default case.

JK
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
James Moe
[PHP] Re: I am stumped
April 30, 2018 01:10AM
On 04/28/2018 09:16 PM, Jeffry Killen wrote:

> The following is the result:
> testBed is the directory being read for this
>
What is the OS's listing of the directory? We have no way of knowing
really what is there and if it matches your results.

> Why is testBed/test_1 included and the value is 1
> instead of 'directory'.
>
How do you initialize "self::$_dirsToRead" before calling
getDirListSingle()?

--
James Moe
jmm-list at sohnen-moe dot com
Think.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jeffry Killen
Re: [PHP] I am stumped
April 30, 2018 07:10PM
> On Apr 29, 2018, at 4:02 PM, James Moe <[email protected]> wrote:
>
> On 04/28/2018 09:16 PM, Jeffry Killen wrote:
>
>> The following is the result:
>> testBed is the directory being read for this
>>
> What is the OS's listing of the directory? We have no way of knowing
> really what is there and if it matches your results.
>
>> Why is testBed/test_1 included and the value is 1
>> instead of 'directory'.
>>
> How do you initialize "self::$_dirsToRead" before calling
> getDirListSingle()?


I solved this.
I was using self::$_dirsToRead for another purpose as well as this
and as a static variable it was retaining values from the other use.
So I created a different static variable for this purpose. Now it is
working as expected.

Thank you for time and attention and thoughts

JK


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login