Solved

scan another directory using windows os

Posted on 2013-01-15
9
171 Views
Last Modified: 2013-01-15
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

0
Comment
Question by:rgb192
  • 4
  • 3
  • 2
9 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 38778953
I don't understand - your second piece of code looks like it should work, so what are you asking?
0
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 38778983
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

0
 

Author Comment

by:rgb192
ID: 38780013
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\')) {
0
 
LVL 11

Expert Comment

by:Slimshaneey
ID: 38780074
What's in that folder? Is there any files in there or just other folders?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:rgb192
ID: 38780158
files and other folders
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 38780642
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);
?>
0
 

Author Comment

by:rgb192
ID: 38780923
Opened the folder successfully!
Array ( )


I have tried with many folders
with and without  the trailing
/
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 38781214
OH - I see the problem:

if ($entry != "." && $entry != ".." && is_file($entry)) {

It's because $entry is simply the filename, so "C:/gyb/file.txt" will just be "file.txt". You need to include the directory in the is_file() function:

if ($entry != "." && $entry != ".." && is_file("C:/gyb/".$entry)) {
0
 

Author Closing Comment

by:rgb192
ID: 38781255
thanks

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


http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27996910.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now