[英]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.我正在尝试使用 PHP 在网络位置检索新创建的文件夹名称,但没有成功。 I have tried 2 different approaches:我尝试了两种不同的方法:
System error 5 has occurred.发生系统错误 5。 Access is denied.访问被拒绝。
path.ps1 :路径.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 : pathtst.php :
<?php
$output = shell_exec('powershell.exe -command c:\scripts\path.ps1');
echo ($output);
?>
Fatal error: Uncaught exception 'com_exception' with message 'Source: WSHNetwork.MapNetworkDrive致命错误:未捕获的异常“com_exception”,消息为“来源: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 ' 在 C:\\HostingSpaces\\test1\\testdomain.com\\wwwroot\\pathtst.php:10 堆栈跟踪:#0 C:\\HostingSpaces\\test1\\testdomain.com\\wwwroot\\pathtst.php(10): com->MapNetworkDrive( 'X:', '\\hostname...', false, 'administrator', 'password') #1 {main} 被抛出到 C:\\HostingSpaces\\test1\\testdoamin.com\\wwwroot\\pathtst.php 第 10 行
pathtst.php : 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.此代码适用于像 c:\\folder 这样的本地驱动器,但不适用于映射的网络驱动器。
Please note that I'm new to PHP and Powershell.请注意,我是 PHP 和 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,它拥有列出目录、网络或其他方式所需的一切。
<?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 ].我几乎从 PHP 文档中逐字复制了readdir
[参考]。
There are a several things that you need to know.有几件事你需要知道。
DIRECTORY_SEPARATOR
, which on Windows returns the appropriate slash in a path name, but for your purposes, it seems unnecessary. PHP 有一个名为DIRECTORY_SEPARATOR
的常量,它在 Windows 上返回路径名中的适当斜杠,但出于您的目的,它似乎没有必要。 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.您需要注意的是,Windows 上的普通目录斜杠被解释为转义字符,因此,如果您像这样键入目录路径, c:scriptspath.ps1
解释的内容是c:scriptspath.ps1
;这也是您获得Access Denied
错误。 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.最适合您的目的是按照我在上面的代码中指示的方式编写它,并使用额外的斜杠来提供您想要的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.