0

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

blackStar
  • 339
  • 4
  • 8
  • 18
  • PHP is server side, so anything you do in PHP will happen on the server. You can't launch files on a client PC with it. Look into Javascript, or a desktop app. – Nathan Loding Oct 18 '11 at 01:10
  • What are you *really* trying to do? Whatever it is it seems like a bad fit for a client-(web) server application. – deceze Oct 18 '11 at 01:36
  • this is how it is supposed to work, and works on the wamp server. User logs to his/her account online --> selects from a list of applications to be launched --> database updates request and writes a file on the F drive (which is on the same domain as the server) the path for the applications to be launched --> after that all u have to do is click launch and starts the batch file. Again the problem is it works when I was doing it on my computer, but once on the actual host server it doesn't seem to write the file. I understand php is only server side, just want to know why it works on wamp? – blackStar Oct 18 '11 at 02:05
  • @Michael The "drive F:" is always as seen from the machine that the server is running on. It works locally because there PHP is running on the same physical machine and has access to the full system (as far as permissions allow yadayada). If you want to access another machine, you are bound to the same network restrictions/allowances/possibilities as anything else. If you can't access the share on the target machine, you can't. SMB in particular is tricky from PHP. Why can't you simply offer the file for *download*? – deceze Oct 18 '11 at 02:12
  • @deceze Ture. I was just thinking since the host server and the client machine are on the same LAN, I could still access the F drive by just calling it \\servername\sharename\userId\\finename. If that doesn't work I will have to just write the file and download it like you said. I was just hoping to avoid having to download the file every time a change is made by the user, also not every user knows where they are saving it and how to run the batch file. The irony is I work in a call center providing tech support for internet. Thanks – blackStar Oct 18 '11 at 02:49

1 Answers1

0

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");
DaveRandom
  • 87,921
  • 11
  • 154
  • 174