Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

scan another directory using windows os

I am using php for windows, not looking for linux answer

this works in same directory
<?php

$filesarray = array();

if ($handle = opendir('.')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && is_file($entry)) {
            //echo "$entry<br />";
            $filesarray[] = $entry;
        }
    }
    closedir($handle);
}

print_r($filesarray);
?>

Open in new window


how can I make this work for another directory
<?php

$filesarray = array();

if ($handle = opendir('C:/gyb/')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && is_file($entry)) {
            echo "$entry<br />";
            $filesarray[] = $entry;
        }
    }
    closedir($handle);
}

print_r($filesarray);
?>

Open in new window

Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

I don't understand - your second piece of code looks like it should work, so what are you asking?
Try this, Ive removed the trailing slash

Use this:

<?php

$filesarray = array();

if ($handle = opendir('C:\gyb')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && is_file($entry)) {
            echo "$entry<br />";
            $filesarray[] = $entry;
        }
    }
    closedir($handle);
}

print_r($filesarray);
?>

Open in new window

Avatar of rgb192

ASKER

no output with these 3:

if ($handle = opendir('C:\gyb')) {

if ($handle = opendir('C:/gyb')) {

if ($handle = opendir('C:/gyb/')) {




parse error with the
'
if ($handle = opendir('C:\gyb\')) {
What's in that folder? Is there any files in there or just other folders?
Avatar of rgb192

ASKER

files and other folders
Try this:

<?php

$filesarray = array();

if(!is_readable('C:/gyb'))
{
  echo "You don't have enough permissions to access this folder!";
  die();
}

if ($handle = opendir('C:/gyb/')) {
    echo "Opened the folder successfully!<br>\n";
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && is_file($entry)) {
            echo "$entry<br />";
            $filesarray[] = $entry;
        }
    }
    closedir($handle);
}
else
{
  echo "There was an error opening the folder!";
  die();
}

print_r($filesarray);
?>
Avatar of rgb192

ASKER

Opened the folder successfully!
Array ( )


I have tried with many folders
with and without  the trailing
/
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

thanks

I have a related question because
I also want to read the folders and put the folders in a separate array


https://www.experts-exchange.com/questions/27996910/I-also-want-to-read-the-folders-and-put-the-folders-in-a-separate-array.html