Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

scan another directory using windows os

Posted on 2013-01-15
9
Medium Priority
?
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 35

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:Slimshaneey
ID: 38780074
What's in that folder? Is there any files in there or just other folders?
0
 

Author Comment

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

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 35

Accepted Solution

by:
gr8gonzo earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

715 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