?
Solved

Can I use PHP to list the contents of a UNC path?

Posted on 2009-12-22
12
Medium Priority
?
1,863 Views
Last Modified: 2012-05-08
I'm building a web application in PHP that needs to list the files of a folder that resides in SharePoint. The SharePoint document library has WebDav enabled so I can access the folder through a unc path (e.g. \\server\Docs\Documents\Folder )

Listing a directory on the web server is pretty straightforward, see the attached code snippet.

The problem is when I try to use the same code with the UNC path, I get an error:
Warning: opendir(\\server\Docs\Documents\Folder) [function.opendir]: failed to open dir

I'm sure this has something to do with the fact that you need domain credentials to access these documents. Is there any way to get these files from PHP?
if (is_dir($directory)) 
{
  if ($dh = opendir($directory)) 
  {
    while (($file = readdir($dh)) !== false)  
    {
	echo $file . "<br>\n";
     }
   }
    closedir($dh);
}

Open in new window

0
Comment
Question by:DVation191
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26107489
0
 
LVL 20

Author Comment

by:DVation191
ID: 26107539
realpath: Returns the canonicalized absolute pathname on success
pathinfo: Returns an array    containing information about the path

I'm sorry, but I don't understand how you'd like me to apply these functions as a solution to my problem. Could you explain further?

0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 200 total points
ID: 26107783
Sorry I misunderstood the question (concentrating too much on the code fragment).

Have you tried using the PEAR WebDav client?

http://pear.php.net/package/HTTP_WebDAV_Client/docs

Or Daniweb has an example of two classes - class_xml and class_http and a worked example of using them in MS environments

http://www.daniweb.com/code/snippet216708.html

My apologies for the earlier confusion.
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 5

Accepted Solution

by:
innotionent earned 900 total points
ID: 26113189
Yes you absolutely need the correct credentials to log into a shared drive.

what I did to access a shared drive was to first create a folder on my linux box. then mount the shared network drive on that folder with cifs/samba.

I've attached some basic code that should get you started.
change the code to fit your needs. and also you can add the code to your fstab so that it mounts whenever you reboot the machine. keep in mind that you might want to use a password for better security since the password will  be in plain text in the fstab file.
sudo mount -t cifs '//192.168.1.1/shared folder' /home/mydir/myfolder -o username=domain/user,password=mypassword

Open in new window

0
 
LVL 18

Assisted Solution

by:Matthew Kelly
Matthew Kelly earned 900 total points
ID: 26113511
If you are running Apache on a Windows server...

To access network resources, the user Apache runs as will need to have permissions to those resources. It defaults to LOCAL SYSTEM user account which does not have network access permissions. To do this:

1. Create a new Windows user. This user account will be used to run the Apache service.
2. Give that user permission to the folders you want access to. It will also need Administrator priviledges on the server Apache runs on. If this is not secure enough for you I am sure there is a list of exact permissions the windows user needs, but start off with Admin group just to make sure it works.
3. go to the Services (Start -> Control Panel -> Admin Tools -> Services) and change the user Apache logs in on to the user account
4. Restart Apache and try to access the resource.

0
 
LVL 20

Author Comment

by:DVation191
ID: 26138301
Okay, just for testing purposes I used an administrator account to set that apache service "log on as" permissions. I then mapped the UNC path to a network drive. I used x: in this example.

If I use "c" as the drive letter (my local OS), the code snippet works fine - so the shell_exec has permissions to do a directory listing. But if I try to use "x", shell_exec returns nothing. I don't understand why this would work on a local hard drive but not a mapped drive.

See the code below.



$drive = 'x';
$path = '';
$path_check =  $drive . ':\\' . $path;	
if (!is_dir($path_check))
{
  echo 'The path requested does not exist';
} 
else
{	
  $cmd = 'dir ' . '"' . $drive . ':\\' . $path . '"' . ' /b /o:n';	
  $dir_list = shell_exec($cmd);
  $dir_array =  explode("\n", trim($dir_list));	
  print_r($dir_array);
}

Open in new window

0
 
LVL 5

Expert Comment

by:innotionent
ID: 26138498
instead of using the drive letter. try using the ip address
0
 
LVL 20

Author Comment

by:DVation191
ID: 26144819
I gave it a try, it processed a little longer, but still gave the same "failed to open dir" warning

This is absolutely frustrating!
0
 
LVL 5

Expert Comment

by:innotionent
ID: 26145343
Are you running php on windows or linux?

If you're on linux, did you try mounting the shared drive to a folder?

in any case it sounds like a permissions problem.
0
 
LVL 20

Author Comment

by:DVation191
ID: 26145777
I'm on windows - my apologies if I didn't make that clear earlier.
0
 
LVL 5

Expert Comment

by:innotionent
ID: 26146402
Alrite. check the acl on the folder you are trying to open.
make sure that the user is in the list of allowed users with appropriate permissions checked to do what you need to do.
I imagine you'll need at least list and read permissions for your script.

0
 
LVL 20

Author Comment

by:DVation191
ID: 26170405
I never really figured out why this didn't work, but after moving the web server to another machine that was using the same credentials that had permissions to the box and the network share as well as the ability to start the apache service, it all worked fine.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

809 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