php: opendir($path) 如何访问网络路径?

gadflyyy 2010-07-14 03:20:30
请教各位大侠,

我用 opendir ( string path )遍历指定路径下的文件,
如果path是本地的文件路径,没有任何问题。
问题是,如果path 类似 “\\192.168.10.10\pictures\”($path = '\\\\192.168.10.10\\pictures\\';),为什么就不能访问呢?该如何解决?

Warning: opendir(\\192.168.10.10\pictures\) [function.opendir]: failed to open dir: No error in xxxx\pic_finder.php on line 141


谢谢!
...全文
223 点赞 收藏 9
写回复
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
gadflyyy 2010-07-14
搞定了,要给apache设一下登陆用户:
参考----
Secondly, the APACHE service must be set-up to take this user into account. If no user is specified, APACHE uses an anonymous user and this is where the problem/error message is coming from. One must go in control panel->administrative tools->services. One will see the list of services and must look for APACHE with Apache/2.X.X in the description. (For Wampserver, it will be called wampapache, and so on!) Right click on that and pop up the properties screen. In the tab LOG ON, there are 2 options: local system account and "this account" which will be a user specified account. This is where one must specify the USER_TEST and PASS_TEST.
----

结贴喽@
php板块第一次提问,
谢谢各位!
回复
gadflyyy 2010-07-14
还是没搞定,,,,网络驱动器的问题,在运行里面net use K: \\192.168.10.10\pictures /persistent:no 就可以创建成功;但是在php里执行 system('net use K: \\192.168.10.10\pictures /persistent:no'); 就不行~~~

再贴点个人认为这方面比较有用的资料
-----------
http://www.dynamicdrive.com/forums/showthread.php?t=27880
Prefered Solution: Map a network drive using PHP at the beginning of the script

This solution is taken from Matt Grimm's posts on the opendir() page of the manual at PHP.net


1) Ensure Apache (or IIS, or php.exe if your running the CLI) is running as an account that has permission to access the relevant resource (I think this is the major sticking point here, the SYSTEM account doesn't have access to mapped network drives!!, but this step alone does not solve the problem). I just ran it as "Administrator", but if you are worried about the (slight) security implications there, set up a new user account and run it under that. You can change this setting in Control Panel > Administrative Tools > Services, select the relevant service and get the properties, then select the "Log On" tab.


2) AT THE BEGINNING of your script, map the network drive with your PHP code, using the windows "net" command's "use" function (you can map network drives from cmd with this command as well... type "net /?" at a command prompt for a basic explanation of this powerful command).


Here's the line: (Matt Grimm got this slightly wrong)


Code:
system('net use Z: "\\servername\sharename" PASSWORD /user:USERNAME /persistent:no');
Notes:
Use the system() command, not exec(). No idea why, but it doesn't work if you use exec(), although the command executes successfully. I haven't tried passthru(), but then why you would use it in this context I don't know...


You can use any unused drive letter. "Z:" is just an example.


If your share name contains a space, you MUST surround the path in the command with double quotes. It's good practice to do this when specifying any path for a Windows command. You must also keep the single quotes in the PHP command, and you not need to escape any back slashes. You can use an IP address instead of a name for the server if you wish.


Yes, it goes password, then username. Also there is no "/password" tag. Wierd, I know, but that's Microsoft for you.

Obviously there is a bit of a security issue with storing the password in your code, essentially in plain text, so here is a nice (albeit long) tutorial on a reversible password encryption method for PHP.


The "/persistent:no" part is essential. The network drive you map from PHP will not be visible in explorer, even if you specify your account as the logon account for Apache. If you don't turn "/persistent" off then it is still there, invisible, after a reboot, and the re-mapping attempt when you run the script again gets an error. You can, however, run the script multiple times during one session in Windows/PHP (ie one boot) without the error.


3) You can now access your network drive from a PHP command! Remember to escape any back slashes ( \ becomes \\ ) or you can just use forward slashes...

Z:/an/example/path

...is perfectly valid. (take note, because this makes writing cross-plaform scripts a lot easier and helps you avoid all those "spent an hour trying to find the problem and forgot to escape something" incidents)



This solution has been tested
with: Apache/2.0.63 (Win32) PHP/5.2.8
on: Windows XP Professional SP2/SP3, Windows Server 2003 R2 (various editions)

However, since the issue is basically an NT auth problem, this should work with any version of PHP (including command line) and any NT based OS.

--------------------------------------------------------------------------------
Last edited by DaveRandom; 03-30-2009 at 09:31 PM.
回复
CunningBoy 2010-07-14
在线的PHP手册的确不错,有很多开发者的提示、经验和技巧在上面。
回复
gadflyyy 2010-07-14
[Quote=引用 1 楼 cunningboy 的回复:]
PHP code
system('net use K: \\192.168.10.10\pictures /user:username password /persistent:no');

$share = opendir('\\\\192.168.10.10\\pictures);
[/Quote]


发完帖子就在这里找到了,,,
http://php.net/manual/en/function.opendir.php
---------------
matt
21-Feb-2009 10:50
I tried it as in the post before described, but it still won't work. Maybe cause i'm using an Apache-Server on Windows 2003 Server.

Now, i found a way that it works and i got access to the share. First of all, you need to set the run user for Apache to a user that has access to the share (see in post before).
Then map your drive in the php script before you read the dir:

system('net use K: \\servername\sharename /user:username password /persistent:no');

$share = opendir('\\\\servername\\sharename');
....
--------------
mana at averna dot com
07-Dec-2007 11:02
I was trying to access network drives using this opendir function. I read so many posts saying that it was almost impossible to access a network drive and finally, I found the answer; there are 2 steps to be followed to access a network drive with PHP either on the same machine or another machine.

But first of all, here's the error I was getting:
Warning: opendir(\\server\folder1\sub_folder) [function.opendir]: failed to open dir: No error in C:\wamp\www\PMOT\v0.1\REPORT_MENU\index.php on line 17

Firstly, one must make sure that the folder \\server\folder1\sub_folder can be accessed by a user, let's say USER_TEST with a password PASS_TEST. By setting properties to this folder, one can add this given user with the correct password (USER_TEST with PASS_TEST).

Secondly, the APACHE service must be set-up to take this user into account. If no user is specified, APACHE uses an anonymous user and this is where the problem/error message is coming from. One must go in control panel->administrative tools->services. One will see the list of services and must look for APACHE with Apache/2.X.X in the description. (For Wampserver, it will be called wampapache, and so on!) Right click on that and pop up the properties screen. In the tab LOG ON, there are 2 options: local system account and "this account" which will be a user specified account. This is where one must specify the USER_TEST and PASS_TEST.

Following these steps worked perfectly for me but if either the folder privileges or apache log on user is disabled, then I get the initial aforementioned error message.

Anyways, I hope this can help out someone!

Cheers!

Marc
--------------------------------------
php官方下面的User Contributed Notes还真不少,,,
试验一下,成功了回来结贴,
谢谢大家!
回复
Dleno 2010-07-14
有人已经给出代码了,呵呵
回复
Dleno 2010-07-14
共享文件一般都是需要在对方的计算机里设置访问帐号的(用本机帐号或来访的计算机帐号)
一般都是设置的为匿名访问帐号:guest
如果是对方机子的帐号,你需要用对方机子的帐号调用系统命令登陆后才能访问。
如果是来访机子的帐号,就用对方设置的对应的来访帐号访问。
只能匿名访问,就只有通过guest来访问了。

首先是要对方允许文件共享的访问,然后再是帐号的问题,
即使这样,你也必须把运行web服务器的帐号改为访问对方共享的帐号,如guest.或通过调用系统命令来实现访问。
说的不太明白,表达有限,我的知识也差。
总的来说就是网络访问权限的问题。
回复
kyzy_yy_pm 2010-07-14
[Quote=引用 1 楼 cunningboy 的回复:]
PHP code
system('net use K: \\192.168.10.10\pictures /user:username password /persistent:no');

$share = opendir('\\\\192.168.10.10\\pictures);
[/Quote]+学习了
回复
kyzy_yy_pm 2010-07-14
没见引号的路径
回复
CunningBoy 2010-07-14
system('net use K: \\192.168.10.10\pictures /user:username password /persistent:no');

$share = opendir('\\\\192.168.10.10\\pictures);
回复
发动态
发帖子
基础编程
创建于2007-09-28

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
社区公告
暂无公告