Solved

scan another directory using windows os

Posted on 2013-01-15
9
174 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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