Link to home
Start Free TrialLog in
Avatar of tancat
tancatFlag for United States of America

asked on

change directory to network drive in php

Hello!

In a php script, how do I change the working directory to a network drive?  

At work I have a web page running on a windows7 pc using WAMPServer.  I'm logged into that computer as me, and I have full access to the shared network drive (windows 2003).  The pc where I'm accessing the web page is also windows7 and I'm logged in as me.  

If I use chdir() and go up or down from the directory where the web page is running, it works fine.  But if I enter a network drive, such as \\dev_server\folder_name, it throws errors.  PHP is not installed on \\dev_server, nor is it installed on the pc where I'm accessing the web page.  

I echo'ed the directory and then copied it into an explorer window and the correct directory comes up (so no typos).  

If I use chdir(), I get the error message, "failed to open dir: No such file or directory".  If I use opendir(), I additionally get the error message, "Access is denied. (code: 5)".  If I use is_dir(), it returns false.  

I found a similar question on Stack Exchange (which was not answered) that suggested the use of exec() but that did not work either - although I might not be using it correctly; I tried various versions of exec("pushd \\dev_server\folder_name"); including using variables, exec($action.$path);, with and without double quotes, etc.  If I open a command line on either windows7 pc, I can pushd to the network directory just fine.  

What the heck am I doing wrong?

By the way, I'm a php newbie (my background is in database development).

Thank you!
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

You're probably not doing anything wrong except thinking that you would be allowed to do that.  Apache, your web server, runs under very limited permissions on purpose.  Both it and IIS are not normally allowed to connect to files on another computer.  Some people have suggested making it a 'mapped drive' so it appears to belong to the computer that the web server is on.  That doesn't always work either.
To access network drive apache has to run as user that can access network drive.
In addition mmap and sendfile options must be turned off to serve static files from that network drive.
Just make a domain account without any permissions past that network drive and you be fine.
Even if the account has access to the network path, PHP script won't be able to pick up the files from there simply because PHP doesn't understand the concept of it.
I have had a project having a similar requirement, but it's a PHP script running from command line rather than a webpage.  My solution was to map the network path to a drive letter, and then PHP can access it without problem.

To map a network path to a drive letter in a .bat file:
REM re-map network drive
subst Z: /D
subst Z: \\file_server_name\sub_directory_name_if_applicable

Open in new window

Avatar of tancat

ASKER

I will try a mapped drive.  Mapping is not 100% but the customers are internal and the worst that could happen is a user's drive gets disconnected and support has to go map it again.

So I will change this question slightly -

Basically I have an existing web page, changed from htm to php because the page contains several links and rather than have a different 'called file' for each link, I am passing a parameter from the link that tells the 'called file' which link the user selected.  (Dave - this is the same project as this question: https://www.experts-exchange.com/questions/28628828/Pass-value-from-html-to-batch-file.html)

I have gone back and forth between making this 'called file' a batch file or a php file.  Currently it is a php file and I'm having the described problems navigating the network.

The reason I changed it from a batch file is because the batch file was not picking up the value of the parameter passed from the main php web page.  Otherwise, everything else in the batch file did what I wanted it to do (rename files, call external programs, move files, etc.).  

So, I guess I don't care if this file is batch or php, I just need to pass it a parameter and then I need it to navigate to different folders on the network.  

What would be a proper way to do this?

(I have a slight preference for php because I like learning it. :-)  Clearly I'm still understanding it's function in web development.)
There is no 'proper' way to do it because web servers are designed to prevent you from doing that.  They are designed with the basic expectation that your files will be in the 'proper' directories in the 'web root' and not somewhere else.  You can make 'aliases' and 'virtual directories' on the same computer the web root and server are on but there is no simple provision for files and directories on other machines.
Run CMD.EXE as that account
NET.EXE USE X: //server/share /permanent

Restart service....
Avatar of tancat

ASKER

Haha - I always seem to be trying to do something that I shouldn't be doing. :-)  This is what happens when you wander into IT and no one kicks you out.  

Ok, to back up further, my company transmits several reports via sftp and ftp to partner vendors.  My predecessors created various batch files that the users would double click to rename, ftp, and archive reports.  One of them created a php program launched from my existing html page that did sort of the same thing.

My boss asked me to simplify and standardize the processes so that they would be easier to maintain, and he created a mockup for the bottom of the existing html page for me to work from.  

We looked at a few companies/products that did both encryption and s/ftp transmission but the boss did not like any of them (it's possible we don't have much budget for this project; I am between Oracle projects right now).  

So I'm inclined at this point to suffer with multiple 'called batch files' so that my boss can have his links in the html page, and change the web page back to htm from php.  

However, if you were handed this project, how would you solve it?

gheist - I will look at this shortly; I may have questions.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tancat

ASKER

A php program would probably be ideal, but I don't think I have enough knowledge at the moment to implement it.  Once I'm actually finished with this project, though, I think that would be a good next step.

I am still working on various combinations of files to eliminate as much duplicate code as possible, in between my other duties, but I will have some quiet today and tomorrow and hopefully make some progress.  

A lesson that I've learned: if something doesn't work, write it down; otherwise when my brain becomes frazzled with everything else going on, I won't remember that I tried something and I'll try it again.  

Thanks!
Avatar of tancat

ASKER

Ok, what I currently have is the existing web page, which sends a parameter value to another php file which sets various variable values and then calls a batch file.  

Everything seems to be communicating ok.  However, the output from the batch file is displayed in the web browser (specifically, if there is output (echo) from the php file, everything from the php file and the batch file is displayed in the browser; however, if I remove the output from the php file, nothing is displayed).

How do I get it to open an actual command window for the user, as well as perform the steps in the batch file?  

This requirement is because the new process can't change very much from the current process from the users' point of view.  They want to be able to see which step the process is on (renaming, encrypting, archiving, etc.) and to "hit any key" to start the next step.  Or if it errors, they send a screen shot before closing the window.  But they don't want to enter any commands.
Avatar of tancat

ASKER

Sorry, I got pulled away earlier and hit Submit without including what I currently have.

I am calling the batch script within the php file this way:
$output=shell_exec('abc.cmd '.$a.' '.$b.' '.$c.' '.$d.' '.$e.' '.$f);

Open in new window


The reason why everything is displaying in the browser window is because I then
echo $output

Open in new window

.

The output indicates that the batch file is running correctly (up until the "hit any key..." part), I just need it to actually happen in an open, non-minimized command window for the user to interact with.
Avatar of tancat

ASKER

I should also mention that abc.cmd is in the same folder that the web page and the php file are in.
Avatar of tancat

ASKER

Ok, I had an unofficial talk with someone and I don't need to have a batch window actually open, I can just display the output as it is currently working, so I think I am set.  I'm going to accept the recommendation to create a command line PHP program.  I always appreciate the answer to "should I do this" or "what should I really do" in addition to "how do I do this".
Avatar of tancat

ASKER

Thank you muchly for your help!
You're welcome.