Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read file on a network drive

Tags:

php

networking

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

theservername\thelocaluser
Warning: fopen(X:\text.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.

like image 486
user1985014 Avatar asked Jan 16 '13 20:01

user1985014


2 Answers

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.

like image 123
Rudi Visser Avatar answered Oct 05 '22 07:10

Rudi Visser


For network shares you should use UNC names: "//server/share/dir/file.ext"

If you use the IP or hostname it should work fine:

$isFolder = is_dir("\\\\NAS\\Main Disk");
var_dump($isFolder); //TRUE

$isFolder = is_dir("//NAS/Main Disk");
var_dump($isFolder); //TRUE

$isFolder = is_dir("N:/Main Disk");
var_dump($isFolder); //FALSE
like image 43
Lawrence Cherone Avatar answered Oct 05 '22 09:10

Lawrence Cherone