如何使用 php 从服务器映射到本地电脑

发布于 2024-12-10 14:15:14 字数 765 浏览 0 评论 0原文

我正在开发一个项目,该项目将使用户能够从网页启动工具。该页面是使用php设计的。我的 PC 上安装了 WAMP 服务器,我可以单击一个按钮在 C 驱动器上创建和写入一个新的批处理文件,其中包含要启动的应用程序的路径,当用户在页面上选择它们时,这些应用程序将从数据库中提取。 现在,当我在我的 PC 上的 WAMP 服务器上执行此操作时,它就可以工作了。我必须将脚本上传到我工作的位于同一 LAN 上的服务器。唯一的问题是我似乎无法在用户电脑上创建或写入文件。

据我了解,它没有映射当前电脑的驱动器。这是我在电脑上时的做法。

$file_path="C\\test.bat"
function runApp($file_path, $appPath){
  $bat_file = fopen($file_path, "w");
  fwrite($bat_file, "start  ".$appPath."  \n");
  fwrite($bat_file, "echo exit  \n");
  fclose($bat_file); //close file after updating batch file
}

为了使其在工作中的服务器上工作,我尝试了

$ip=$_SERVER['REMOTE_ADDR'];
$file_path="user:pass@".$ip."\\F:\\test.bat";

但它仍然无法工作。我缺少什么?任何帮助将不胜感激

,或者请告诉我是否有任何其他方式可以启动批处理文件或来自 php 站点的任何文件

I am working on a project that will enable user to launch tools from a webpage. the page is designed using php. I have WAMP server installed on the my PC and I can click a button to create and write a new batch file on the C drive with the path of applications to be launched which are pulled from the db when the user selects them on the page.
Now this works when I am doing it on the WAMP server on my PC. I have to upload the script to a server at my work that is hosted on the same LAN. only problem is I can't seem to be able to create or write to a file on the user PC.

From my understanding it is not mapping the drive for the current PC. here is how I did when it was on my PC.

$file_path="C\\test.bat"
function runApp($file_path, $appPath){
  $bat_file = fopen($file_path, "w");
  fwrite($bat_file, "start  ".$appPath."  \n");
  fwrite($bat_file, "echo exit  \n");
  fclose($bat_file); //close file after updating batch file
}

in order to make it work from the server at work, I tried

$ip=$_SERVER['REMOTE_ADDR'];
$file_path="user:pass@".$ip."\\F:\\test.bat";

but it is still not working. What am I missing? Any help would be greatly appreciated

or please let me know if there is any other way I could launch a batch file or any file form the php site

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三人与歌 2024-12-17 14:15:14

我假设您知道实际上无法从服务器上的 PHP 脚本在客户端 PC 上执行批处理文件(至少不能单独使用 PHP),因此我不会对此进行解释。

我还假设您正在处理所有 Windows 计算机 - 从问题中的代码示例和上下文看来很可能如此。


您在这里寻找的是处理 SMB 协议的函数。有几种方法可以处理这个问题:

  • 很多时候,您可以简单地使用 UNC(正如您在上面尝试做的那样)。正确的语法是 \\servername\sharename\file.ext。我遇到过有人声称您可以使用 username:password@ 作为前缀(还有人声称您可以使用 @username:password 作为后缀,这肯定是错误的)但我都无法工作。因此,如果您想使用此方法来完成这项工作,我建议您以 AD 中具有在客户端计算机上读/写相关共享的权限的用户身份运行 PHP,因此不需要额外的身份验证。< /p>

  • 这个类我一直想尝试一段时间,它为 PHP 提供了 smb:// 流包装器。我对此没有任何经验,但从表面上看,您应该能够使用标准 URL 语法来访问该文件(即 smb://user:pass@/servername/sharename/file.txt)。 ext)。

  • 您可以映射网络驱动器,以便可以像任何其他驱动器号一样使用它。这是我成功用于访问需要 PHP 身份验证的 SMB 共享的唯一方法。此方法有其缺点,因为它增加了另一层复杂性 - 由于您临时映射驱动器号,因此您必须确保多个并发实例使用不同的驱动器号。

基本程序是这样的:

// Set the parameters
$serverNameOrIp = $_SERVER['REMOTE_ADDR'];
$shareName = 'f';
$fileName = 'test.bat';
$userName = 'Dave';
$password = 'Random';
$driveLetter = 'Z'; // You have to make sure there are no clashes with this!

// Map the drive
exec("net use {$driveLetter}: \\\\{$serverNameOrIp}\\{$shareName} {$password} /user:{$userName} /persistent:no");

// Do your thang... for example
file_put_contents("{$driveLetter}:\\$fileName",$batchFileData);

// Un-map the drive to avoid conflicts with later instances
exec("net use {$driveLetter}: \\\\{$serverNameOrIp}\\{$shareName} /delete");

I am assuming that you are aware that you cannot actually execute the batch file on the client PC from the PHP script on the server (at least, not with PHP alone), so I won't get into explaining that.

I am also assuming that you are dealing with all Windows machines - as seems likely from the code samples in, and the context of, the question.


What you are looking for here is functions to handle the SMB protocol. There are a couple of ways to handle this:

  • A lot of the time, you can simply use a UNC (as you appear to be trying to do above). The correct syntax for this would be \\servername\sharename\file.ext. I have come across people claiming that you can prefix this with username:password@ (and one claim that you can suffix it with @username:password, which surely must be wrong) but I cannot get either to work. Therefore, if you want to use this method to do the job, I would recommend that you run PHP as a user that has permissions in AD to read/write the relevant share on the client machine, so additional auth will not be required.

  • There is this class I have been meaning to try out for some time, which provides an smb:// stream wrapper for PHP. I don't have any experience with this, but from the looks of it you should be able to use a standard URL syntax to access the file (i.e. smb://user:pass@/servername/sharename/file.ext).

  • You can map a network drive so that it is available to use like any other drive letter. This is the only method I have ever successfully used to access an SMB share that requires authentication from PHP. This method has it's drawbacks, as it adds another layer of complication - since you are temporarily mapping a drive letter, you have ensure that multiple concurrent instances use different drive letters.

The basic procedure would go something like this:

// Set the parameters
$serverNameOrIp = $_SERVER['REMOTE_ADDR'];
$shareName = 'f';
$fileName = 'test.bat';
$userName = 'Dave';
$password = 'Random';
$driveLetter = 'Z'; // You have to make sure there are no clashes with this!

// Map the drive
exec("net use {$driveLetter}: \\\\{$serverNameOrIp}\\{$shareName} {$password} /user:{$userName} /persistent:no");

// Do your thang... for example
file_put_contents("{$driveLetter}:\\$fileName",$batchFileData);

// Un-map the drive to avoid conflicts with later instances
exec("net use {$driveLetter}: \\\\{$serverNameOrIp}\\{$shareName} /delete");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文