Windows directory listings

This is a discussion on Windows directory listings within the PHP General forums, part of the PHP Programming Forums category; Hi, I am trying to write a script that reads a directory on Windows. All the PHP functions I have ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-06-2007
Beauford
 
Posts: n/a
Default Windows directory listings

Hi,

I am trying to write a script that reads a directory on Windows. All the PHP
functions I have looked at all seem to work with the Linux dietary
structure. Is there another way to do this.

Thanks
Reply With Quote
  #2 (permalink)  
Old 01-06-2007
Veign
 
Posts: n/a
Default Re: Windows directory listings

Something like this:

$dh = @opendir ($Somepath);
while ($file = @readdir ($dh)) {
if (($file!="..") && ($file!=".")) {
echo $file;
}
}

--
Chris
http://www.veign.com / http://www.veign.com/blog
--
""Beauford"" <phpuser@netscan.ca> wrote in message
news:000001c731c0$61149850$123aa8c0@netscan.ca...
> Hi,
>
> I am trying to write a script that reads a directory on Windows. All the
> PHP
> functions I have looked at all seem to work with the Linux dietary
> structure. Is there another way to do this.
>
> Thanks



Reply With Quote
  #3 (permalink)  
Old 01-07-2007
Jochem Maas
 
Posts: n/a
Default Re: [PHP] Windows directory listings

Beauford wrote:
> Hi,
>
> I am trying to write a script that reads a directory on Windows. All the PHP
> functions I have looked at all seem to work with the Linux dietary


it sounds more like you have found an examples that show windows
being used (i.e. windows file paths).

> structure. Is there another way to do this.


you must be reading a manual that only exists in your particular parallel
universe, all relevant php function work in windows as well as linux:

http://php.net/manual/en/ref.filesystem.php
http://php.net/dir

>
> Thanks
>

Reply With Quote
  #4 (permalink)  
Old 01-07-2007
Beauford
 
Posts: n/a
Default RE: [PHP] Windows directory listings

Maybe I should clarify. When I use a windows path (c:\whatever) I get an
error that it can't find the path. In Linux the same code works fine
(/usr/local/whatever).

Here is all the relevent info.

Warning: opendir(f:\downloads\): failed to open dir: Invalid argument in
c:\web\index.php on line 30

***** This is line 30 - if ( $handle = opendir ( $page ) )

This is the code:

$page = "f:\\downloads";
// $mode : "FULL"|"DIRS"|"FILES"
// $d : must not be defined

$display = searchdir($page);

$num = count($display);
$i = 0;

for($i = 0; $i <= $num; $i++) { echo $i." ".$display[$i]."<br>"; }



function searchdir ( $page , $maxdepth = -1 , $mode = "FILES" , $d = 0 ) {
if ( substr ( $page , strlen ( $page ) - 1 ) != '\\' ) { $page .= '\\' ;
}
$dirlist = array () ;
if ( $mode != "FILES" ) { $dirlist[] = $page ; }
if ( $handle = opendir ( $page ) )
{
while ( false !== ( $file = readdir ( $handle ) ) )
{
if ( $file != '.' && $file != '..' )
{
$file = $file ;
if ( ! is_dir ( $file ) ) { if ( $mode != "DIRS" ) {
$dirlist[] = $file ; } }
elseif ( $d >=0 && ($d < $maxdepth || $maxdepth < 0) )
{
$result = searchdir ( $file . '\\' , $maxdepth , $mode ,
$d + 1 ) ;
$dirlist = array_merge ( $dirlist , $result ) ;
}
}
}
closedir ( $handle ) ;
}
if ( $d == 0 ) { natcasesort ( $dirlist ) ; }
return ( $dirlist ) ;
}

?>

> -----Original Message-----
> From: Jochem Maas [mailto:jochem@iamjochem.com]
> Sent: January 7, 2007 10:25 AM
> To: Beauford
> Cc: PHP
> Subject: Re: [php] Windows directory listings
>
> Beauford wrote:
> > Hi,
> >
> > I am trying to write a script that reads a directory on

> Windows. All
> > the PHP functions I have looked at all seem to work with the Linux
> > dietary

>
> it sounds more like you have found an examples that show
> windows being used (i.e. windows file paths).
>
> > structure. Is there another way to do this.

>
> you must be reading a manual that only exists in your
> particular parallel universe, all relevant php function work
> in windows as well as linux:
>
> http://php.net/manual/en/ref.filesystem.php
> http://php.net/dir
>
> >
> > Thanks
> >

>
>
>

Reply With Quote
  #5 (permalink)  
Old 01-08-2007
Jochem Maas
 
Posts: n/a
Default Re: [PHP] Windows directory listings

with regard to trolling - I'd don't *just* do that :-)

sometime I even get the answer to the question correct :-)
let's see if I can help ...

Beauford wrote:
> Maybe I should clarify. When I use a windows path (c:\whatever) I get an
> error that it can't find the path. In Linux the same code works fine
> (/usr/local/whatever).
>
> Here is all the relevent info.
>
> Warning: opendir(f:\downloads\): failed to open dir: Invalid argument in
> c:\web\index.php on line 30
>
> ***** This is line 30 - if ( $handle = opendir ( $page ) )
>
> This is the code:
>
> $page = "f:\\downloads";


you got this part right (escaping the backslash)

> // $mode : "FULL"|"DIRS"|"FILES"
> // $d : must not be defined
>
> $display = searchdir($page);
>
> $num = count($display);
> $i = 0;
>
> for($i = 0; $i <= $num; $i++) { echo $i." ".$display[$i]."<br>"; }
>
>
>
> function searchdir ( $page , $maxdepth = -1 , $mode = "FILES" , $d = 0 ) {
> if ( substr ( $page , strlen ( $page ) - 1 ) != '\\' ) { $page .= '\\' ;}


this if statement will run given the value of $page, it's not needed AFAIKT.
then again I can't see it causing problems.

btw: php defines a constant DIRECTORY_SEPARATOR automatically which may help you
make your code more portable, and save you having to think/worry about the blacksdlashing
'problem'.

> $dirlist = array () ;
> if ( $mode != "FILES" ) { $dirlist[] = $page ; }


right here I suggest adding a check using is_dir() and is_readable() on the $page var.
for now stick in a bit of debug code ...

echo '<pre>';
var_dump($path, is_dir($path), is_readable($path));
echo '</pre>';

I'm guessing that is_readable() is going to return false, which would point
the fact that either the drive or the directory is not readable by the webserver
process (you don't mention whether you are running via a the webserver but I assume you
are given the output your generating include a '<br>') - the situation maybe compounded
by the fact that F: is actually a mapped drive of some sorts (I have no idea
what kind of trouble this could cause).



> if ( $handle = opendir ( $page ) )
> {
> while ( false !== ( $file = readdir ( $handle ) ) )
> {
> if ( $file != '.' && $file != '..' )
> {
> $file = $file ;
> if ( ! is_dir ( $file ) ) { if ( $mode != "DIRS" ) {
> $dirlist[] = $file ; } }
> elseif ( $d >=0 && ($d < $maxdepth || $maxdepth < 0) )
> {
> $result = searchdir ( $file . '\\' , $maxdepth , $mode ,
> $d + 1 ) ;
> $dirlist = array_merge ( $dirlist , $result ) ;
> }
> }
> }
> closedir ( $handle ) ;
> }
> if ( $d == 0 ) { natcasesort ( $dirlist ) ; }
> return ( $dirlist ) ;
> }
>
> ?>
>
>> -----Original Message-----
>> From: Jochem Maas [mailto:jochem@iamjochem.com]
>> Sent: January 7, 2007 10:25 AM
>> To: Beauford
>> Cc: PHP
>> Subject: Re: [php] Windows directory listings
>>
>> Beauford wrote:
>>> Hi,
>>>
>>> I am trying to write a script that reads a directory on

>> Windows. All
>>> the PHP functions I have looked at all seem to work with the Linux
>>> dietary

>> it sounds more like you have found an examples that show
>> windows being used (i.e. windows file paths).
>>
>>> structure. Is there another way to do this.

>> you must be reading a manual that only exists in your
>> particular parallel universe, all relevant php function work
>> in windows as well as linux:
>>
>> http://php.net/manual/en/ref.filesystem.php
>> http://php.net/dir
>>
>>> Thanks
>>>

>>
>>

>

Reply With Quote
  #6 (permalink)  
Old 01-08-2007
Beauford
 
Posts: n/a
Default RE: [PHP] Windows directory listings



> -----Original Message-----
> From: Jochem Maas [mailto:jochem@iamjochem.com]
> Sent: January 7, 2007 8:35 PM
> To: Beauford
> Cc: 'PHP'
> Subject: Re: [php] Windows directory listings
>
> with regard to trolling - I'd don't *just* do that :-)
>
> sometime I even get the answer to the question correct :-)
> let's see if I can help ...
>
> Beauford wrote:
> > Maybe I should clarify. When I use a windows path

> (c:\whatever) I get
> > an error that it can't find the path. In Linux the same code works
> > fine (/usr/local/whatever).
> >
> > Here is all the relevent info.
> >
> > Warning: opendir(f:\downloads\): failed to open dir:

> Invalid argument
> > in c:\web\index.php on line 30
> >
> > ***** This is line 30 - if ( $handle = opendir ( $page ) )
> >
> > This is the code:
> >
> > $page = "f:\\downloads";

>
> you got this part right (escaping the backslash)
>
> > // $mode : "FULL"|"DIRS"|"FILES"
> > // $d : must not be defined
> >
> > $display = searchdir($page);
> >
> > $num = count($display);
> > $i = 0;
> >
> > for($i = 0; $i <= $num; $i++) { echo $i." ".$display[$i]."<br>"; }
> >
> >
> >
> > function searchdir ( $page , $maxdepth = -1 , $mode =

> "FILES" , $d = 0 ) {
> > if ( substr ( $page , strlen ( $page ) - 1 ) != '\\' ) {

> $page .=
> > '\\' ;}

>
> this if statement will run given the value of $page, it's not
> needed AFAIKT.
> then again I can't see it causing problems.


This makes no difference. I took it out before and got the same error.


> btw: php defines a constant DIRECTORY_SEPARATOR automatically
> which may help you make your code more portable, and save you
> having to think/worry about the blacksdlashing 'problem'.
>
> > $dirlist = array () ;
> > if ( $mode != "FILES" ) { $dirlist[] = $page ; }

>
> right here I suggest adding a check using is_dir() and
> is_readable() on the $page var.
> for now stick in a bit of debug code ...
>
> echo '<pre>';
> var_dump($path, is_dir($path), is_readable($path)); echo '</pre>';


I checked the NTFS permissions and they are fine, and the local directory
displays fine. By this I mean the directory where this script is.

So if the script is in c:\test I can read c:\test, c:\test\test2.
c:\test\test3, ../test2. etc. It only happens when I go outside of the
current drive.

So if I put $page = "test", or $page = "test3", or $page = "../" they will
all work. As soon as I add a drive letter, it gives me the error. Even on
the local machine.

I am trying to access a networked drive, but I have a share on the PC where
the script is . May this is the issue. Is there a way to use a UNC path?

> I'm guessing that is_readable() is going to return false,
> which would point the fact that either the drive or the
> directory is not readable by the webserver process (you don't
> mention whether you are running via a the webserver but I
> assume you are given the output your generating include a
> '<br>') - the situation maybe compounded by the fact that F:
> is actually a mapped drive of some sorts (I have no idea what
> kind of trouble this could cause).
>
>
>
> > if ( $handle = opendir ( $page ) )
> > {
> > while ( false !== ( $file = readdir ( $handle ) ) )
> > {
> > if ( $file != '.' && $file != '..' )
> > {
> > $file = $file ;
> > if ( ! is_dir ( $file ) ) { if ( $mode != "DIRS" ) {
> > $dirlist[] = $file ; } }
> > elseif ( $d >=0 && ($d < $maxdepth ||

> $maxdepth < 0) )
> > {
> > $result = searchdir ( $file . '\\' , $maxdepth ,
> > $mode , $d + 1 ) ;
> > $dirlist = array_merge ( $dirlist , $result ) ;
> > }
> > }
> > }
> > closedir ( $handle ) ;
> > }
> > if ( $d == 0 ) { natcasesort ( $dirlist ) ; }
> > return ( $dirlist ) ;
> > }
> >
> > ?>
> >
> >> -----Original Message-----
> >> From: Jochem Maas [mailto:jochem@iamjochem.com]
> >> Sent: January 7, 2007 10:25 AM
> >> To: Beauford
> >> Cc: PHP
> >> Subject: Re: [php] Windows directory listings
> >>
> >> Beauford wrote:
> >>> Hi,
> >>>
> >>> I am trying to write a script that reads a directory on
> >> Windows. All
> >>> the PHP functions I have looked at all seem to work with

> the Linux
> >>> dietary
> >> it sounds more like you have found an examples that show windows
> >> being used (i.e. windows file paths).
> >>
> >>> structure. Is there another way to do this.
> >> you must be reading a manual that only exists in your particular
> >> parallel universe, all relevant php function work in

> windows as well
> >> as linux:
> >>
> >> http://php.net/manual/en/ref.filesystem.php
> >> http://php.net/dir
> >>
> >>> Thanks
> >>>
> >>
> >>

> >

>
> --
> PHP General Mailing List (http://www.php.net/) To
> unsubscribe, visit: http://www.php.net/unsub.php
>
>
>

Reply With Quote
  #7 (permalink)  
Old 01-08-2007
Jochem Maas
 
Posts: n/a
Default Re: [PHP] Windows directory listings

Beauford wrote:
>



....

>>>
>>>
>>> function searchdir ( $page , $maxdepth = -1 , $mode =

>> "FILES" , $d = 0 ) {
>>> if ( substr ( $page , strlen ( $page ) - 1 ) != '\\' ) {

>> $page .=
>>> '\\' ;}

>> this if statement will run given the value of $page, it's not
>> needed AFAIKT.
>> then again I can't see it causing problems.

>
> This makes no difference. I took it out before and got the same error.
>


I didn't think it would.

....

> So if I put $page = "test", or $page = "test3", or $page = "../" they will
> all work. As soon as I add a drive letter, it gives me the error. Even on
> the local machine.
>
> I am trying to access a networked drive, but I have a share on the PC where
> the script is. May this is the issue.


the mapped drive should not be a problem. I definitely think it's a permission issue,
of course I could be wrong.

the following bug report *might* give you a clue as to whats going on:

http://bugs.php.net/bug.php?id=22153&edit=1

I have a feeling the bit about setting the permissions of the Share
to Everyone (on the server) is a good place to start. (the point about
having to login to the share manually before php is able to connect is also
noteworthy)

also bare in mind that a Drive/Directory has a set of permissions that are
seperate to the permissions of a Share that is defined for said Drive/Directory,
(something that has always confused me a bit) - both need to be setup to appropriately

> Is there a way to use a UNC path?


things I came across suggests this can work - something like?:

$dir = opendir(""\\\\your.ip.add.ress\\your_share"");

>


....

>

Reply With Quote
  #8 (permalink)  
Old 01-08-2007
Myron Turner
 
Posts: n/a
Default Re: [PHP] Windows directory listings

Jochem Maas wrote:
> Beauford wrote:


>>
>> I am trying to access a networked drive, but I have a share on the PC where
>> the script is. May this is the issue.

>
> the mapped drive should not be a problem. I definitely think it's a permission issue,
> of course I could be wrong.
>
>

What are the permissions set on the mapped drive? That's what will
determine access.

--

_____________________
Myron Turner
http://www.mturner.org/XML_PullParser/
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:34 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0