我試圖在使用PHP的網絡位置檢索新創建的文件夾名稱,但沒有成功。我嘗試了兩種不同的方法:如何使用PHP列出網絡驅動器中的目錄?
- 在PHP中使用Powershell獲取文件夾名稱。我得到了以下錯誤:
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);
?>
- 從PHP運行映射網絡驅動器並獲取文件夾名稱。這結束了與錯誤:
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);
?>
此代碼與一個本地驅動器,如C效果很好:\文件夾,但不能與映射網絡驅動器。
請注意,我是PHP和Powershell的新手。
不能使用UNC名稱嗎? (例如:「//server/share/dir/file.exension」) 然後使用,is_dir,fopen等 – Brice