Solved

scan another directory using windows os

Posted on 2013-01-15
9
170 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
What's in that folder? Is there any files in there or just other folders?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:rgb192
Comment Utility
files and other folders
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 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 …

744 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

12 Experts available now in Live!

Get 1:1 Help Now