how to map on a local pc from a server using php

1.4k views Asked by At

I am working on a project that will enable user to launch tools from a webpage. the page is designed using php. I have WAMP server installed on the my PC and I can click a button to create and write a new batch file on the C drive with the path of applications to be launched which are pulled from the db when the user selects them on the page. Now this works when I am doing it on the WAMP server on my PC. I have to upload the script to a server at my work that is hosted on the same LAN. only problem is I can't seem to be able to create or write to a file on the user PC.

From my understanding it is not mapping the drive for the current PC. here is how I did when it was on my PC.

$file_path="C\\test.bat"
function runApp($file_path, $appPath){
  $bat_file = fopen($file_path, "w");
  fwrite($bat_file, "start  ".$appPath."  \n");
  fwrite($bat_file, "echo exit  \n");
  fclose($bat_file); //close file after updating batch file
}

in order to make it work from the server at work, I tried

$ip=$_SERVER['REMOTE_ADDR'];
$file_path="user:pass@".$ip."\\F:\\test.bat";

but it is still not working. What am I missing? Any help would be greatly appreciated

or please let me know if there is any other way I could launch a batch file or any file form the php site

1

There are 1 answers

0
DaveRandom On

I am assuming that you are aware that you cannot actually execute the batch file on the client PC from the PHP script on the server (at least, not with PHP alone), so I won't get into explaining that.

I am also assuming that you are dealing with all Windows machines - as seems likely from the code samples in, and the context of, the question.


What you are looking for here is functions to handle the SMB protocol. There are a couple of ways to handle this:

  • A lot of the time, you can simply use a UNC (as you appear to be trying to do above). The correct syntax for this would be \\servername\sharename\file.ext. I have come across people claiming that you can prefix this with username:password@ (and one claim that you can suffix it with @username:password, which surely must be wrong) but I cannot get either to work. Therefore, if you want to use this method to do the job, I would recommend that you run PHP as a user that has permissions in AD to read/write the relevant share on the client machine, so additional auth will not be required.

  • There is this class I have been meaning to try out for some time, which provides an smb:// stream wrapper for PHP. I don't have any experience with this, but from the looks of it you should be able to use a standard URL syntax to access the file (i.e. smb://user:pass@/servername/sharename/file.ext).

  • You can map a network drive so that it is available to use like any other drive letter. This is the only method I have ever successfully used to access an SMB share that requires authentication from PHP. This method has it's drawbacks, as it adds another layer of complication - since you are temporarily mapping a drive letter, you have ensure that multiple concurrent instances use different drive letters.

The basic procedure would go something like this:

// Set the parameters
$serverNameOrIp = $_SERVER['REMOTE_ADDR'];
$shareName = 'f';
$fileName = 'test.bat';
$userName = 'Dave';
$password = 'Random';
$driveLetter = 'Z'; // You have to make sure there are no clashes with this!

// Map the drive
exec("net use {$driveLetter}: \\\\{$serverNameOrIp}\\{$shareName} {$password} /user:{$userName} /persistent:no");

// Do your thang... for example
file_put_contents("{$driveLetter}:\\$fileName",$batchFileData);

// Un-map the drive to avoid conflicts with later instances
exec("net use {$driveLetter}: \\\\{$serverNameOrIp}\\{$shareName} /delete");