Searching Network Drives

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slonison
Forum Newbie
Posts: 13
Joined: Thu Jun 28, 2007 6:50 pm

Searching Network Drives

Post by slonison »

Ok here's the deal. I go to a college where there are a few servers on campus... What I want to do is search these servers with php and add certain traits about them to a mysql database. There's no problem with the adding them to the database so long as I have the file name.

The thing I'm having a problem with is actually finding the files.

Now I tried this code for my server.

Code: Select all

<?php

$Array = glob('\\\\myserver\\Shared\\*', GLOB_ONLYDIR);
   
while(list ($key,$value) = each($Array))
   {
       echo $key . ' : ' . $value . '<br />';
   }

?>
Indexing went just fine. But when I tried...

Code: Select all

<?php

$Array = glob('\\\\servername\\Shared\\*', GLOB_ONLYDIR);
   
while(list ($key,$value) = each($Array))
   {
       echo $key . ' : ' . $value . '<br />';
   }

?>
It didn't work...I know I have permission to the folder because I access files from there all the time. Not to mention, I'm not the only search engine on the campus, there's another which does mysql databases as well.

The first code returns

Code: Select all

0 : \\myserver\Shared\Comedy
1 : \\myserver\Shared\Emulation
2 : \\myserver\Shared\Movies
3 : \\myserver\Shared\New Releases
4 : \\myserver\Shared\OST
5 : \\myserver\Shared\Programs
6 : \\myserver\Shared\TV Shows
While the second set of code returns a blank white page.

Yes, I've searched through the forums but never got an answer that would work. Thanks alot.

Not to mention, but this only searches the folder I specifiy next to the servername... what I'm actually looking for is a script that can grab all of the shared folders by using just the \\\\servername\* command.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Searching Network Drives

Post by volka »

slonison wrote:I know I have permission to the folder because I access files from there all the time.
Are you sure this script runs with your account's permissions? If you run it through a webserver it might be another account.
slonison
Forum Newbie
Posts: 13
Joined: Thu Jun 28, 2007 6:50 pm

Post by slonison »

ok let me rephrase... Whenever we access the servers... we basically jsut go to Start->Run->'\\servername

this basically just opens windows explorer with the files displayed in the folder where I can drag and drop.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Let's try another question: Exactly how do you invoke the php script?
slonison
Forum Newbie
Posts: 13
Joined: Thu Jun 28, 2007 6:50 pm

Post by slonison »

I put the script online and just type into my browser [mozilla:firefox] the URL of the file.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Meaning: You are running the script through a webserver. The webserver process might work under another account not having the permissions needed to access the network shares.
slonison
Forum Newbie
Posts: 13
Joined: Thu Jun 28, 2007 6:50 pm

Post by slonison »

How do I run it in such a way that it has the same permissions sent as when I access it through Run.

The server is in my home so I have access to everything on it.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

In case you're running an apache webserver on a win32 machine you might be interested in http://httpd.apache.org/docs/2.2/platfo ... tml#winsvc
slonison
Forum Newbie
Posts: 13
Joined: Thu Jun 28, 2007 6:50 pm

Post by slonison »

Yes, I think that's the case because the I installed xampp on a Windows XP Professional Edition. So I'll take a look at the article.
slonison
Forum Newbie
Posts: 13
Joined: Thu Jun 28, 2007 6:50 pm

Post by slonison »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


ok i've read the article but I still don't understand how I can make this code work...

Forgive, me I don't know much about web development but I'm more than willing to learn. I've been trying this...

Code: Select all

<?

$con = mysql_connect("localhost","username","password");
if (!$con)
{
  die('Could Not Connect: ' . mysql_error());
}
mysql_select_db("bbb",$con);
$sql="TRUNCATE TABLE server";
if (!mysql_query($sql,$con))
{
  die('Error: ' . mysql_error());
}



             $servername = "//servername/Shared/";
             $dir0 = opendir($servername);
             $i=1;
             while (($file = readdir($dir0)) != false)
             {
                 if($file != "." && $file != "..")  //If the file isn't root the directory
                 {
                     if($file != "." && $file != "..")
                                 {
                                     $path1 = $servername . $file;
                                     $dir1 = opendir($path1);
                                     while (($file1 = readdir($dir1)) != FALSE)
                                     {
                                         if($file1 != "." && $file1 != "..")
                                         {
                                             $path2 = $path1 . "/" . $file1;
                                             $dir2 = opendir($path2);
                                             while (($file2 = readdir($dir2)) != FALSE)
                                             {
                                                 if(stristr($file2, ".") != FALSE && $file2 != "." && $file2 != ".." && $file2 != "Thumbs.db")
                                                 {
                                                     $path3 = "file:///" . $path2 . "/";
                                                     $size=substr(filesize($path2."/".$file2)/1000000,0,4) . " MB";
                                                     $ext=substr($file2,-4);
                                                     $sql="INSERT INTO server (Name, SubDir1, SubDir2, SubDir3, Path, Size, Type) VALUES ('$file2','$file','$file1','','$path3','$size','$ext')";
                                                     if (!mysql_query($sql,$con))
                                                     {
                                                        die('Error: ' . mysql_error());
                                                     }

                                                     echo $file . " " . " " . $file2 . "<font color=red> Added</font>". "<br />";
                                                 }
                                             }
                                         }
                                     }
                                 }
                    }
              }
                                         

             closedir($dir0);

             mysql_close($con);

?>
The code only works if servername is the name of the computer that the server is run off of...


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Have you changed to account settings for the webserver from LocalSystem to something that has access to the network?
http://httpd.apache.org/docs/2.2/platform/windows.html#winsvc wrote:By default, all Apache services are registered to run as the system user (the LocalSystem account). The LocalSystem account has no privileges to your network via any Windows-secured mechanism, including the file system, named pipes, DCOM, or secure RPC.
slonison
Forum Newbie
Posts: 13
Joined: Thu Jun 28, 2007 6:50 pm

Post by slonison »

Ok, I understand that it's the credentials and stuff from my apache setup, but can you inform me how to change these settings?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Follow the link volka kindly provided. Read the section under his quoted text.
slonison
Forum Newbie
Posts: 13
Joined: Thu Jun 28, 2007 6:50 pm

Post by slonison »

Ok I'm sorry if it's getting annoying to help me out. But I'm still having trouble understanding what they have written.
1. Create a normal domain user account, and be sure to memorize its password.
Where and how do I do this? Do I do this the same way as creating a new user account for the computer?
2. Grant the newly-created user a privilege of Log on as a service and Act as part of the operating system. On Windows NT 4.0 these privileges are granted via User Manager for Domains, but on Windows 2000 and XP you probably want to use Group Policy for propagating these settings. You can also manually set these via the Local Security Policy MMC snap-in.
3. Confirm that the created account is a member of the Users group.
4. Grant the account read and execute (RX) rights to all document and script folders (htdocs and cgi-bin for example).
5. Grant the account change (RWXD) rights to the Apache logs directory.
6. Grant the account read and execute (RX) rights to the Apache.exe binary executable.
No clue at all how to do the rest. The group policy thing I took a look at but it seems really complicated, and if somebody on the devnetwork forums know, I'd rather not go in blindly and change something I shouldn't.

Thanks
Post Reply