Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

php - Read file on a network drive

I'm running Xampp on a Windows Server ; Apache is running as a service with a local account. On this server, a network share is mounted as X: with specific credentials.

I want to access files located on X: and run the following code

<?php
echo shell_exec("whoami");
fopen('X:\text.txt',"r");
?>

and get

theservernamehelocaluser
Warning: fopen(X:ext.txt) [function.fopen]: failed to open stream: No such file or directory

I tried to run Apache, not as a service but directly by launching httpd.exe ... and the code worked.

I can't see what causes the difference between the service and the application and how to make it works.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're not able to do this using a drive letter, as network mapped drives are for a single user only and so can't be used by services (even if you were to mount it for that user).

What you can do instead is use the UNC path directly, for example:

fopen('\\server\share\text.txt', 'r');

Note, however, that there are a few issues with PHP's filesystem access for UNC paths. One example is a bug I filed for imagettftext, but there are also issues with file_exists and is_writeable. I haven't reported the latter because as you can see from my long-outstanding bug with imagettftext, what's the point.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...