简体   繁体   中英

How to list the directories in a network drive using PHP?

I'm trying to retrieve the newly created folder name in a network location using PHP with no success. I have tried 2 different approaches:

  1. Getting the folder name using Powershell in PHP. I got the following error:

System error 5 has occurred. Access is denied.

path.ps1 :

net use Y: "\\hostname\folder" /user:administrator 'password'  /Persistent:Yes /y
$tat = Get-ChildItem -Directory Y:\folderlist | sort CreationTime -Descending | select -First 1 

pathtst.php :

<?php
$output = shell_exec('powershell.exe -command c:\scripts\path.ps1');
echo ($output);
?>
  1. mapping network drive on the run from PHP and getting the folder names. This ended with the error:

Fatal error: Uncaught exception 'com_exception' with message 'Source: WSHNetwork.MapNetworkDrive
Description: The network name cannot be found. ' in C:\\HostingSpaces\\test1\\testdomain.com\\wwwroot\\pathtst.php:10 Stack trace: #0 C:\\HostingSpaces\\test1\\testdomain.com\\wwwroot\\pathtst.php(10): com->MapNetworkDrive('X:', '\\hostname...', false, 'administrator', 'password') #1 {main} thrown in C:\\HostingSpaces\\test1\\testdoamin.com\\wwwroot\\pathtst.php on line 10

pathtst.php :

<?php
$letter = 'X';
$WshNetwork = new COM("WScript.Network");
$WshNetwork->MapNetworkDrive("$letter:", '\\hostname\folder', FALSE,     'administrator', 'password'); 
$dir = 'X:\folderlist';
$tblname = scandir($dir);

print_r($tblname);
?>

This code works well with a local drive like c:\\folder, but not with a mapped network drive.

Please note that I'm new to PHP and Powershell.

From what I see, you are making this very hard on yourself.

If you are using PHP, it has everything you need to list a directory, network or otherwise.

<?php
if($fh=opendir('\\\\path\\to\\files')) {
  while (false !== ($fi =readdir($fh))) {
    echo "$fi\n";
  }
}
?>

I pretty much copied this verbatim from the PHP docs for readdir [ reference ].

There are a several things that you need to know.

  1. PHP does not have access to mapped drives as the drive letter. That is set per user and not made available in the OS environment. This you need to point directly to the network path.
  2. PHP has a constant called DIRECTORY_SEPARATOR , which on Windows returns the appropriate slash in a path name, but for your purposes, it seems unnecessary. What you need to be aware of is that normal directory slashes on Windows are interpreted as the escape characters, so if you type a directory path like you did, what gets interpreted is c:scriptspath.ps1 ;which is also why you get the Access Denied error. Best thing for your purposes would be to write it how I indicated in my code above with the extra slashes that will give you what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM