简体   繁体   English

我正在尝试使用php从Windows连接到Linux上的Samba共享文件夹

[英]I'm trying to connect from windows to Samba Shared folder on linux using php

I have php on Windows 7 and I'm trying to connect to Samba Shared folder on Linux but no success. 我在Windows 7上安装了php,但正在尝试连接到Linux上的Samba共享文件夹,但未成功。

I'm using this code: 我正在使用此代码:

// Map the drive
system('net use Y: "\\\\linuxIPaddress\Shared_folder password /user:username /persistent:no>nul 2>&1');
// Open the directory
$dir = "Y:/TMP";
if (is_dir($dir)) {
if (opendir($dir)) {
    print "able to access directory tree.";
}
} else {
print "not access tree.";
}

i found solution for connection using this code 我找到了使用此代码的连接解决方​​案

$sharename = '\\\\LinuxIPAddress\Shared_folder';
$username = 'username';
$password = 'password';
$letter = 'Z:';    

if (!is_dir($letter . "/TMP")) {
    $WshNetwork = new COM("WScript.Network");
    $WshNetwork->MapNetworkDrive($letter, $sharename, FALSE, $username, $password);
}

and is working perfect 并且工作完美

This is more of a comment, but I don't have enough reputation to comment yet, so: @DanielMiovski 's solution did work for me as well. 这更多的是评论,但我没有足够的声誉来发表评论,因此:@DanielMiovski的解决方案也对我有用。 Once I mapped it in the script I was able to manipulate the files I wanted to. 一旦将其映射到脚本中,便可以操纵所需的文件。 However, I ran into a problem. 但是,我遇到了一个问题。 On Windows 10, once the script ended the drive was not deleted from my network drives. 在Windows 10上,脚本结束后,该驱动器并未从我的网络驱动器中删除。 I could not manually disconnect it via explorer nor the command prompt because: "The network connection does not exist." 我无法通过资源管理器或命令提示符手动断开连接,因为:“网络连接不存在。” I searched for many solutions to this on the internet and finally got it to go away, but it took way to much effort for something that simple. 我在互联网上搜索了许多解决方案,最后使它消失了,但是为了简单起见,它花费了很多精力。 I still wanted to use this code in my script, because it was the only smb solution for php that actually worked for me. 我仍然想在脚本中使用此代码,因为它是php真正为我工作的唯一smb解决方案。 I found a solution to both problem. 我找到了两个问题的解决方案。 Use @Daniel Miovski 's script above, but at the end, add this line: 在上面使用@Daniel Miovski的脚本,但最后添加以下行:

system('net use Z: /delete /y');

This deletes the network drive as soon as the script is done manipulating it. 一旦脚本完成操作,这将删除网络驱动器。 I have not had any troubles of this kind since. 从那以后,我再也没有遇到过这种麻烦。

Just thought that I would post this in case anyone else was running into this same problem. 只是以为我可以将其发布,以防其他人遇到同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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