Jump to content

Help in open directory


javedkarim

Recommended Posts

Hi,

I am using WAMP server for php and mysql and running windows XP. here is the link to wamp sever: http://www.wampserver.com/en/

I am trying to list all the files in the directory and here is my code

 

<?php
//Open images directory
$dir = opendir("C:\wamp\www\tutorials\directory") or die ("File cant be opened");

echo "<br />";
echo "-----> $dir ";
echo "<br />";

//List files in images directory
while (($file = readdir($dir)) !== false)
  {
  echo "filename: " . $file . "<br />";
  }

closedir($dir);
?> 

 

When i ran the code it gives me error:

File cant be opened 

 

Its really annoying. I have tried several time to use opendir() function every time i fails. please tell me what is the error. 

Link to comment
Share on other sites

Thanks for help

 

Add the following two lines after your <?php tag to get php to show any errors that occur when opendir() executes -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

I have done as you have said:

 

<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
//Open images directory
$dir = opendir("C:\wamp\www\tutorials\directory") or die ("File cant be opened");

echo "<br />";
echo "-----> $dir ";
echo "<br />";

//List files in images directory
while (($file = readdir($dir)) !== false)
  {
  echo "filename: " . $file . "<br />";
  }

closedir($dir);
?> 

 

It gives me this error

 


Warning: opendir(C:\wamp\www utorials\directory) [function.opendir]: failed to open dir: No error in C:\wamp\www\tutorials\directory\dir_list_1.php on line 5
File cant be opened

Link to comment
Share on other sites

Thanks it worked  :)

 

I guess it considers the "\t" as tab. Escape it by adding another slash: "\\t" (i guess it will work like that), or just use relative directories the linux way with a backslash: "tutorials/directory".

 

it works

$dir = opendir("C:\wamp\www\\tutorials\directory");
or 
$dir = opendir("C:\\wamp\\www\\tutorials\\directory");

 

But it did not worked with network files i tried to use it on network directories see below. Note rest code is same as below.

$dir = opendir("\\192.168.62.35\TV Series\House MD\Season 4");
or
$dir = opendir("\\192.168.62.35\\TV Series\\House MD\\Season 4");

 

It gives me error


Warning: opendir(\192.168.62.35\TV Series\House MD\Season 4) [function.opendir]: failed to open dir: No error in C:\wamp\www\tutorials\directory\dir_list_1.php on line 6



Warning: readdir(): supplied argument is not a valid Directory resource in C:\wamp\www\tutorials\directory\dir_list_1.php on line 15

Warning: closedir(): supplied argument is not a valid Directory resource in C:\wamp\www\tutorials\directory\dir_list_1.php on line 20

 

 

I have used Ip address. Is it the right way to list the network directory files? or there are other ways to list the network directories. please help me.

 

Link to comment
Share on other sites

Apache/PHP (can't remember which one off the top of my head -> i think it's Apache) will translate a linux-delimited path into a windows one for you...

 

This allows you to use

 

"C:/wamp/www/tutorials/directory"

 

So you don't have to escape anything. Alternately, if you had used single quotes

opendir('C:\wamp\www\tutorials\directory')

There probably wouldn't have been an issue. I personally like to use forward slashes for multi-OS compatibility, and so you don't have to escape when using double quotes.

Link to comment
Share on other sites

But it did not worked with network files i tried to use it on network directories see below. Note rest code is same as below.

$dir = opendir("\\192.168.62.35\TV Series\House MD\Season 4");
or
$dir = opendir("\\192.168.62.35\\TV Series\\House MD\\Season 4");

 

This won't work. Your best method is to map a network drive in windows and use that path

My Computer -> Tools -> Map Network Drive

And use that drive letter.

 

Alternately, you should be able to use the \\computername\path\to\file format. This is quite annoying using backslashes, as even with single quotes you have to use

$url = '\\\\computername\path\to\file';

 

With Apache, you can use

 

$url = '//computername/path/to/file';

 

Tested with PHP 5.2.6

 

 

Link to comment
Share on other sites

This won't work. Your best method is to map a network drive in windows and use that path

My Computer -> Tools -> Map Network Drive

And use that drive letter.

 

Alternately, you should be able to use the \\computername\path\to\file format. This is quite annoying using backslashes, as even with single quotes you have to use

$url = '\\\\computername\path\to\file';

 

With Apache, you can use

 

$url = '//computername/path/to/file';

 

Tested with PHP 5.2.6

 

 

All above methods works perfectly with my computer drive directories but not works with Ip method( network computers ) ie.

$dir = opendir('//192.168.62.35/TV Series/House MD/Season 4');

 

Your right about mapping the network drives but i can not map 400+ computers they are too much for network drive and how will i get all 400 computers name.

$url = '//computername/path/to/file';

 

Tell me how i will get all computers name.

Is there any other methods in php and Apache.

 

Link to comment
Share on other sites

You can only access directories using file system paths or ftp.

 

You either map it so that it can be accessed through the file system -

 

/path/to/file.ext

relative/path/to/file.ext

fileInCwd.ext

C:/path/to/winfile.ext

C:\path\to\winfile.ext

\\smbserver\share\path\to\winfile.ext

file:///path/to/file.ext

 

or you need to access it through the ftp protocol (this would require that any computer you wish to access have an FTP server installed) -

 

ftp://example.com/pub/file.txt

ftp://user:password@example.com/pub/file.txt

ftps://example.com/pub/file.txt

ftps://user:password@example.com/pub/file.txt

example.com in the above could be replaced with the IP address of the FTP server.
Link to comment
Share on other sites

All above methods works perfectly with my computer drive directories but not works with Ip method( network computers ) ie.

$dir = opendir('//192.168.62.35/TV Series/House MD/Season 4');

 

I know it won't work that way. I told you it wouldn't.

 

Your right about mapping the network drives but i can not map 400+ computers they are too much for network drive and how will i get all 400 computers name.

$url = '//computername/path/to/file';

 

Tell me how i will get all computers name.

Is there any other methods in php and Apache.

 

Get all computers name? This would be dealing with your domain name server - assuming you have one set up.

 

If you have 400+ computers in your network and don't have a proper name/ip listing then maybe you shouldn't be in charge of the network. If you aren't you might want to talk to the person that is. The only suggestion I can give you here is to get the computer names manually. You can do this fairly easily ( assuming they share a domain/workgroup ) by going into My Network Places or by using the following little script.

 

set_time_limit( 0 );

# Note - the timeout for gethostbyaddr is LONG, so this script can be VERY SLOW on unpopulated ranges.

$prefix = '10.0.0.';
$range = range(1, 30);

foreach( $range as $i ) {
echo $prefix.$i.' = '.gethostbyaddr($prefix.$i)."\n";
flush();
ob_flush();
}

 

This is assuming you have a nameserver set up properly.

 

You could also just use

$dir = opendir('//'.gethostbyaddr('192.168.62.35').'/TV Series/House MD/Season 4');

But again, it might be slow. You're better off with a static list.

Link to comment
Share on other sites

This script will be much faster, but I've found the results differ slightly... for the most part it should be reliable though...

 

<pre><?php
set_time_limit( 0 );

$prefix = '10.0.0.';
$range = range(1, 255);

foreach( $range as $i ) {
echo $prefix.$i.' = '.getWinHost($prefix.$i)."\n";
flush();
ob_flush();
}

function getWinHost( $ip, $fullName = FALSE ) {
preg_match( '%Name:[\s]++(.++)%i', `nslookup $ip`, $m );
if ( !$fullName )
	return reset( explode('.', $m[1]) );
else
	return $m[1];
}
?></pre>

 

This can be done on the fly as well, but as I said before, a static list would be better.

Link to comment
Share on other sites

 

 

I know it won't work that way. I told you it wouldn't.

 

Get all computers name? This would be dealing with your domain name server - assuming you have one set up.

 

If you have 400+ computers in your network and don't have a proper name/ip listing then maybe you shouldn't be in charge of the network. If you aren't you might want to talk to the person that is. The only suggestion I can give you here is to get the computer names manually. You can do this fairly easily ( assuming they share a domain/workgroup ) by going into My Network Places or by using the following little script.

 

set_time_limit( 0 );

# Note - the timeout for gethostbyaddr is LONG, so this script can be VERY SLOW on unpopulated ranges.

$prefix = '10.0.0.';
$range = range(1, 30);

foreach( $range as $i ) {
echo $prefix.$i.' = '.gethostbyaddr($prefix.$i)."\n";
flush();
ob_flush();
}

 

 

Well your script works many thanks its really cool  :-* that was the stuff i was looking for.

 

 

But there is problem there

$dir = opendir('//'.gethostbyaddr('192.168.62.35').'/TV Series/House MD/Season 4');

 

The code is still showing me warning

Warning: opendir(//HYDRAZDUNGEON/TV Series/House MD/Season 4) [function.opendir]: failed to open dir: No error in C:\wamp\www\tutorials\directory\dir_list_1.php on line 6



Warning: readdir(): supplied argument is not a valid Directory resource in C:\wamp\www\tutorials\directory\dir_list_1.php on line 17

Warning: closedir(): supplied argument is not a valid Directory resource in C:\wamp\www\tutorials\directory\dir_list_1.php on line 22

 

Well i am on local area network where every one shares there stuff. Tell me about administrative stuff if its really need to get permission i will defiantly get permission. If its not important leave administrative stuff. Because i have to build search engine i have to focus on that stuff. So please help me to sort the problem of this directory.

 

 

 

Link to comment
Share on other sites

Well, the user running PHP must have access to the remote folder for one.

 

I also don't recommend using gethostbyaddr on the fly. If you MUST though, use it like this

 

<pre><?php

$ip = '10.0.0.3';
$host = gethostbyaddr( $ip );
if ( $ip == $host )
die( 'Unable to resolve hostname from ip '.$ip );

$path = '//'.$host.'/SomeDir/';

if ( !is_dir($path) )
die( $path. ' is not a directory' );

$dir = opendir($path);
if ( $dir == FALSE )
die( 'Cannot read '.$path );

while (($file = readdir($dir)) !== FALSE)
echo "filename: $file : filetype: ".filetype( $path.$file)."\n";

closedir( $dir );


?></pre>

Link to comment
Share on other sites

Thanks for the reply

Well, the user running PHP must have access to the remote folder for one.

 

I also don't recommend using gethostbyaddr on the fly. If you MUST though, use it like this

 

<pre><?php

$ip = '10.0.0.3';
$host = gethostbyaddr( $ip );
if ( $ip == $host )
die( 'Unable to resolve hostname from ip '.$ip );

$path = '//'.$host.'/SomeDir/';

if ( !is_dir($path) )
die( $path. ' is not a directory' );

$dir = opendir($path);
if ( $dir == FALSE )
die( 'Cannot read '.$path );

while (($file = readdir($dir)) !== FALSE)
echo "filename: $file : filetype: ".filetype( $path.$file)."\n";

closedir( $dir );


?></pre>

 

here is my replaced statment

$ip = '192.168.62.35';
$path = '//'.$host.'/Dethklok - The Dethalbum (2007)/';
or
$path = '//'.$host.'/Dethklok - The Dethalbum (2007)';

Well i have tried your script. At

if ( !is_dir($path) )

die( $path. ' is not a directory' );

 

line it dies.

here is the error message.

//HYDRAZDUNGEON/Dethklok - The Dethalbum (2007) is not a directory

 

even directory exists and directory full path is

\\192.168.62.35\Dethklok - The Dethalbum (2007)

 

Up till now i have figured that the problem is with directory path. If i am giving the wrong path correct me.

Link to comment
Share on other sites

Well, the user running PHP must have access to the remote folder for one.

 

I also don't recommend using gethostbyaddr on the fly. If you MUST though, use it like this

 

<pre><?php

$ip = '10.0.0.3';
$host = gethostbyaddr( $ip );
if ( $ip == $host )
die( 'Unable to resolve hostname from ip '.$ip );

$path = '//'.$host.'/SomeDir/';

if ( !is_dir($path) )
die( $path. ' is not a directory' );

$dir = opendir($path);
if ( $dir == FALSE )
die( 'Cannot read '.$path );

while (($file = readdir($dir)) !== FALSE)
echo "filename: $file : filetype: ".filetype( $path.$file)."\n";

closedir( $dir );


?></pre>

 

Well this script runs fine on my ip address

ie if i replace my ip address with $ip = '10.0.0.3';

 

I think there is sort of server site restriction but in windows XP i go to

start>Run>\\192.168.12.87 or any other ip. Other people sharing are easily accessed even there directories and i can easily copy the file from them. Same applies on my ip if i access my IP address using the same method it also access and other can also copy file form my ip. The problem is when i try to list the files using php from other people IP address it wont let me list them even i can access them but from my ip i can easily list the files please tell me how can i list other people directories. Is Apache has some kind of restriction or there is other problem.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.