DVation191
asked on
Can I use PHP to list the contents of a UNC path?
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\Fo lder )
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\Docu ments\Fold er) [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?
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\Docu
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);
}
ASKER
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?
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?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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);
}
instead of using the drive letter. try using the ip address
ASKER
I gave it a try, it processed a little longer, but still gave the same "failed to open dir" warning
This is absolutely frustrating!
This is absolutely frustrating!
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.
If you're on linux, did you try mounting the shared drive to a folder?
in any case it sounds like a permissions problem.
ASKER
I'm on windows - my apologies if I didn't make that clear earlier.
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.
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.
ASKER
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.
http://www.php.net/realpath
http://www.php.net/pathinfo