Read file on a network drive - php

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.

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.

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

If it helps for the future, what i did was this:
Create a local account with the same username and password as the account which you access to the network drive.
Go to Windows Services > Apache2 > Properties > Login > Log in as this account ... and specify the username and password that you created.
About the syntax, the one that worked for me was:
$fileName = '//192.168.1.10/Folder/file.txt';
I'm using php 8.

Related

Write to file on shared drive from apache server PHP [duplicate]

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.
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.
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
If it helps for the future, what i did was this:
Create a local account with the same username and password as the account which you access to the network drive.
Go to Windows Services > Apache2 > Properties > Login > Log in as this account ... and specify the username and password that you created.
About the syntax, the one that worked for me was:
$fileName = '//192.168.1.10/Folder/file.txt';
I'm using php 8.

php cannot open text file on network drive [duplicate]

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.
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.
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
If it helps for the future, what i did was this:
Create a local account with the same username and password as the account which you access to the network drive.
Go to Windows Services > Apache2 > Properties > Login > Log in as this account ... and specify the username and password that you created.
About the syntax, the one that worked for me was:
$fileName = '//192.168.1.10/Folder/file.txt';
I'm using php 8.

Copy network file from a Linux host to my Linux server using PHP

I am trying to retrieve a network file from an OpenSUSE 13.1 Host to my OpenSUSE 13.2 Webserver, but I don't seem to have any success.
First I check if target directory is a directory:
$path = "\\\\192.168.xxx.xxx\\public";
if(is_dir($path)){ // returns FALSE every time.
return true;
}
In this case the function is_dir() returns FALSE, although the folder exists and has 777 permisions, the IP is correct, and the computer is turned ON.
I have tried all the combinations of formatting the network path, including $path = "smb://192.168.xxx.xxx/public"; witch returns an error
Unable to find the wrapper "smb" - did you forget to enable it when
you configured PHP?
Could someone tell me what I am missing?
Would it work the same through a OpenVPN with my Centos 6 webserver and an Ubuntu 14 host?
PHP has no built-in Samba support on the Linux platform. You will need to mount the remote share (requires superuser-equivalent access) and access its contents like a local filesystem.

PHP is_dir() returns false on Windows network drive

I have a network drive mapped to drive letter X:\ going to an external hard drive with the path of "\\X-Drive\Public\Data".
I am using Zend Server with Apache.
My PHP command is simple
$isFolder = is_dir("x:/");
echo($isFolder); //FALSE
Things you should know:
The code:
$isFolder = is_dir("c:/");
echo($isFolder); //TRUE
works as expected.
I am running the Zend Apache service as an administrator user. I know this is working properly because in Task Manager the httpd.exe process shows the correct user.
The drive is indeed mapped. I have tried mapping it with several users, to include the same user that Zend Apache uses to no avail.
I have read every post on this matter that I could find. Every problem I've come across exists either because of user permissions, or a typo. I don't see how my problem fall into either category.
I have also tried:
system('net use X: "\\x-drive\public" password1 /user:username /persistent:no');
$isFolder(is_dir("x:/"));
echo($isFolder); //FALSE
I am running Windows Vista x64, and the production code will run in Windows 7 x64.
What other problems could I be running in to?
For network shares you should use UNC names:
"//server/share/dir/file.ext" Source
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

php access network path under windows

within PHP (XAMPP) installed on a Windows XP Computer Im trying to read a dir which exists on a local network server. Im using is_dir() to check whether it is a dir that I can read.
In Windows Explorer I type \\\server\dir and that dir is being shown.
When I map a network drive a can access it with z:\dir as well.
In PHP I have that script:
<?php if( is_dir($dir){ echo 'success' } ) ?>
For $dir I tried:
/server/dir
//server/dir
\server\dir
\\server\dir
\\\\server\\dir
and
z:\dir
z:\\dir
z:/dir
z://dir
But I never get success?
Any idea?
thx
I solved it by changing some stuff in the registry of the server as explained in the last answer of this discussion:
http://bugs.php.net/bug.php?id=25805
Thanks to VolkerK and Gumbo anyway!
I love stackoverflow and their great people who help you so incredibly fast!!
EDIT (taken from php.net):
The service has limited access to network resources, such as shares
and pipes, because it has no credentials and must connect using a null
session. The following registry key contains the NullSessionPipes and
NullSessionShares values, which are used to specify the pipes and
shares to which null sessions may connect:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters
Alternatively, you could add the REG_DWORD value
RestrictNullSessAccess to the key and set it to 0 to allow all null
sessions to access all pipes and shares created on that machine.`
add RestrictNullSessAccess=0 to your registery.
You probably let xampp install apache as service and run the php scripts trough this apache. And the apache service (running as localsystem) is not allowed to access the network the way your user account is.
A service that runs in the context of the LocalSystem account inherits the security context of the SCM. The user SID is created from the SECURITY_LOCAL_SYSTEM_RID value. The account is not associated with any logged-on user account.
This has several implications:
...
* The service presents the computer's credentials to remote servers.
...
You can test this by starting the apache as console application (apache_start.bat in the xampp directory should do that) and run the script again. You can use both forward and backward slashes in the unc path. I'd suggest using //server/share since php doesn't care about / in string literals.
<?php
$uncpath = '//server/dir';
$dh = opendir($uncpath);
echo "<pre>\n";
var_dump($dh, error_get_last());
echo "\n</pre>";
Try the file: URI scheme:
file://server/dir
file:///Z:/dir
The begin is always file://. The next path segment is the server. If it’s on your local machine, leave it blank (see second example). See also File URIs in Windows.
Yes, I know this is an old post, but I still found it, and if anyone else does...
On Windows, with newer servers, verify the SMB is installed and enabled on the target machine.

Categories