...

View Full Version : Access a Mapped Driver, via opendir() OR exec()



glish_dreams
12-24-2008, 11:45 PM
I'm needing help getting my PHP to read files on a Mapped Drive.
My Server is Windows 2003.

I can't even execute this simple bat file using
<?php exec(); ?> function



copy "W:\*.csv" "Z:\WFM_XMLs\" /y


Which would be my work around, that would just move the files from the Mapped drive, to a local hard drive.

Fou-Lu
12-25-2008, 12:01 AM
What is you're error when you attempt to read the directory?


Oh yeah, and can you confirm if you're software (IIS or Apache), and whether you're doing this via web or via CLI?

glish_dreams
12-25-2008, 05:49 AM
Im sorry, details...

Windows 2003 , Apache 2.2.10 PHP/5.2.6 with ActivePerl 5.10.0

Loading it as a Web page, should I call php via CMD will that make it work.. im gonna try ..

Im using this basic function to look at all the files in a directory, I know that only the files I need will be in this directory.


<?php
function dirList ($directory) {
$results = array();
$handler = opendir($directory);
while ($file = readdir($handler)) {
if ($file != '.' && $file != '..')
$results[] = $file;
}
closedir($handler);
return $results;
}
?>

I call the function with... Which is a directory of a Mapped network drive, on another Windows 2003 Server. That requires a login, I have the drive saved user and pass and I have it auto reconnect.


<?php
$dirArray=dirList('W:/');
?>

I just get the dumb errors


Warning: opendir(W:/) [function.opendir]: failed to open dir: No error in C:\_web\dev_www\WFMpayroll\dir.php on line 13
Warning: readdir(): supplied argument is not a valid Directory resource in C:\_web\dev_www\WFMpayroll\dir.php on line 15
Warning: closedir(): supplied argument is not a valid Directory resource in C:\_web\dev_www\WFMpayroll\dir.php on line 23

Having a second hard drive on the server with several 100 Gigs, I found that I could just move the files from the 'Shared Drive' to the 'Local Drive', which if I used the Z: drive in the call for the opendir function, it works. I made this work around...
This line works in a .bat file ran on the Windows Server.


copy "W:\*.csv" "Z:\WFM_XMLs\" /y

So I tried using PHP exec and shell_exec, but always get a blank array, I do IPCONFIG and I get all the returned information. I look into the folder just to see if it did it , but didnt echo, nope empty.


<pre>
<?php
exec('copy "W:\*.csv" "Z:\WFM_XMLs\" /y',$output);
print_r($output);
?>
</pre>

I even tried ...
exec('cmd /c [.bat location]');
__

The overall outcome is I need to get the files from anoter Windows Server, to the Windows Server the Apache/PHP is running on, then I need to loop through the files, see if the file name is in the database, if not import the CSV file into the database, else dont do anything with the file.

Which I got a script that loops through searching my db for the file name inserting it if not found, importing it via LOAD DATA LOCAL INFILE

__

Everything works fine on the Local Hard drives, just not the Mapped.
Oddly, I can't execute the .bat file via PHP. ?>?

I think if I can just get PHP to move the files some how that would work out best

I have Telnet enabled on the Target Windows Server, if that can help. Im attempting to think of a route myself. I may be able to enable Telnet on the other server.
I can't relie on Windows Scheduler on this project.

glish_dreams
12-25-2008, 05:51 AM
I think running the php script works, im going to try making exec run the php script.

Fou-Lu
12-25-2008, 06:04 AM
Do try.
I'm wondering if its just a permission problem. Remember that you're shares are combined most restrictive, so you're share permission must be set to a minimum of read, and the directory access must be at minimum read and traverse. I would probably give this privilege to Everybody, even if just to test it out.
You should be able to see what process Apache is running under as well. It may be under a more restricted user that cannot read the provided directory. I find that a good way to test this is by using the absolute sharename path: \\yourserver\yourshare and see if you can connect. I would expect php to throw out no permission errors, but at least you know it can see it.


Y'know, I'm curious if php is choking and considering this to be an external resource. If it is, you may be able to fix it by ensuring that allow_url_fopen is enabled in you're INI file.

glish_dreams
12-25-2008, 06:08 AM
Ok it only works if I do it under windows, via run by PHP-cgi.exe

Now if I use exec to call the php page like this.


<pre>
<?php
exec('cmd /c C:\php\php-5.2.6\php-cgi.exe C:\_web\dev_www\WFMpayroll\dir.php',$output);
print_r($output);
?>
</pre>

I only get



Array( [0] => X-Powered-By: PHP/5.2.6 [1] => Content-type: text/html [2] => [3] => [4] => Array [5] => ( [6] => ) [7] => [8] => [9] => )


So I think I figured out whats up, from watching all of these script run via CLI

I think when I have a web page use exec, it isn't going to walk for it to end before it goes any and give me output. So it ends running the script. if thats the cause, can i make it keep going

glish_dreams
12-25-2008, 06:14 AM
Now when you say set the premissions which 'Folder' do you mean and On which server.

When I set premissions on the Mapped Drive, which is direct to the folder that holds the CSV files. I can only add users that show up on the HOST SERVER. Iv added everyone and system, set to FULL rights. Nothing changed.

I made sure those same premissions were on the HOST SERVER

Fou-Lu
12-25-2008, 06:27 AM
Ok it only works if I do it under windows, via run by PHP-cgi.exe

Do you mean IIS?
If so, it definitely sounds like a permissions problem.

Sounds like you're permissions are set fine. Often its a problem on the host server end (since everything appears well on our ends). Best I know, the actual share permissions are default for full control on everyone, so thats fine, no reason to change those.

I'll check back later, I've got some work to finish in the next hour so I can go home :P

glish_dreams
12-25-2008, 06:33 AM
Do you mean IIS?
If so, it definitely sounds like a permissions problem.

Sounds like you're permissions are set fine. Often its a problem on the host server end (since everything appears well on our ends). Best I know, the actual share permissions are default for full control on everyone, so thats fine, no reason to change those.

I'll check back later, I've got some work to finish in the next hour so I can go home :P


No i dont have IIS installed at all.
Im using apache, loading php via

LoadModule php5_module "C:/php/php-5.2.6/php5apache2_2.dll"
PHPIniDir "C:/php/php-5.2.6"

when i say php-cgi.exe, what I did is right click the .php file open with and tried each of the 3, until it worked, php.exe, php-cgi.exe, php-win.exe


php.ini has allow_url_fopen on no change was needed

Fou-Lu
12-25-2008, 06:43 AM
I would have thought the cli ones would work too (since they are also run as you're user).
I'm not sure what the problem is, and I don't have quite a configuration that allows me to test it out either.
To me, it sounds like it can't access the path via Apache.
Try this out, open you're services (services.msc I believe it is), find Apache and change the login credentials to a specific account. Even for testing, just put it as an administrator or any standard account you can access (don't leave it as an admin of course!). Restart it, see if that fixes it.

When I used to have a server setup with both IIS and Apache, I used IISUser for both so my apache logged in as IISUser and I had no trouble with access.


I'll poke around the net in a little while too to see if I can't find this exact situation.

glish_dreams
12-25-2008, 06:57 AM
no difference with Administrator as the apache runner, I even rebooted.

I even followed .. http://httpd.apache.org/docs/2.0/platform/windows.html

Running Apache as a ServiceNothing made any effect..

Im still finding it strange exec() isn't fixing the issue, well it is, if I open with php directly, instead of using the browser.

But I need to be able to remotely do this.

Fou-Lu
12-25-2008, 08:24 AM
Yeah I'm a little at a loss myself.
There are some extra things you can try. I was actually directed to this from a google search, but check out the user comments for opendir on the PHP website. The first one looks like it fits you're problem to the 'T'. I was close if it does turn out to work ;)

glish_dreams
12-25-2008, 05:53 PM
Ok, so I finaly was able to set it as (Administrator) and rebooted, and it was able to pull the opendir via
$dirArray=dirList('//INTERNAL IP/xml');

Then I setup a new user, made sure all directories had acces, I restarted the service, all my old apps, ALIAS's worked, but not the script.
So I rebooted, the new user wouldnt start the service, kept saying syntax error line 117, and DocumentRoot (which is line 117) must be a directory, [these errrors were in event viewer] . The access rights existed correctly for the new user.

There server is external also, so there is no way I can make Administrator run the service. But I need this solution, which I had with Admin.

What am I doing wrong here. I have complete control over both servers. This is a (intranet) setup, with them both having external IPs

Fou-Lu
12-25-2008, 11:37 PM
Sounds like you're getting closer.
I'm about 80% sure that documentRoot error you're getting is one I had before as well. Windows doesn't like having no drive to map to, and actually halts you're apache if its not available. I was doing this with a flash stick when I was in school and mounting it on a linux computer and mapping it on my windows. I found out when it wasn't available it stopped my apache on windows (though just triggered an error on linux).

With you're administrator account, is its domain the same (or higher) as the remote share? With the user account you created, did you make sure that it has act as a part of the OS and Login as a service privileges (both of these I believe are group policies)?

All and all it just *sounds* like you cannot access the share as you're user OR the Apache service is loading before you're tcpip, afd, svchost for netsvcs, possibly lanmanworkstation (or other network redirection service) or rpcss services. Sadly these dependancies are not my strong point, but I believe you're requiring the tcpip, afd and lanmanworkstation as dependancies before you can run you're apache service and access remote shares.
You'd think that this would be easier by just mapping it eh >.<

Oh, I'm going to move this thread too, since its more of a config problem then a php problem. I'll leave a redirect too.

glish_dreams
12-26-2008, 12:09 AM
The Document Error I was getting was all lies the document root is on a local hard drive
DocumentRoot "C:/_web/www" And like I said the user I made to run apache, it had access to that drive, It had all the access to Run as OS and Log on as Service.
I had the network share Mapped, and had it auto reconnect.

when I use administrator my script works by going to the //INTERNAL IP/share . but still not the mapped drive, which is ok.
Maybe I need to login as that user and log into the shared folder and have it save the password to it also.



Now when you said domain. there isnt one other that the computers names. We dont have Active Directory. Both the servers on are the same subnet though.

Fou-Lu
12-26-2008, 12:27 AM
No Active directory should still be alright. Since you're shared directory is set with share privileges everybody full control, and I'm assuming its either FAT based or NTFS with at least read for Everybody or Annonymous, you're permissions should be fine.

Try always using the unc for the share instead of the mapped name. Without being logged in that could be the cause of you're problems.
Also, can you check you're dependancies for Apache service and post the ones you have? I expect AFP and TcpIP at minimum since I'm fairly certain Apache2 adds these dependencies on its own.

glish_dreams
12-28-2008, 09:01 PM
The dependencies attached to the Apache2.2 server are ( AFD, TCP/IP Protocal Driver > IPSEC driver)


I redid the entire server, complete fresh OS and apache, I can still make the result work with runing as Administrator, but I cant make my newly created user for Apache work until I put it under the (Administrators) group. There is one small step Im over looking. Im going to log into the station as the User i created for Apache and see what that does.

glish_dreams
12-28-2008, 10:39 PM
Ok I think its kinda solved.
Finally I was able to setup a user call (web) that starts the Apache service. I can reboot and it will still run the service. I got all my aliases working to.

I tried my script, no go.
I then logged into the server via remote desktop as (web) then I opened the web share and logged in via RUN.
Logged off, logged back in as Administrator, Reboot the server.
I tried my script, woot we got some action.




So my last question, will one day my script stop working and Ill need to login and access the web share again?

Fou-Lu
12-29-2008, 12:36 AM
Ok I think its kinda solved.
Finally I was able to setup a user call (web) that starts the Apache service. I can reboot and it will still run the service. I got all my aliases working to.

I tried my script, no go.
I then logged into the server via remote desktop as (web) then I opened the web share and logged in via RUN.
Logged off, logged back in as Administrator, Reboot the server.
I tried my script, woot we got some action.




So my last question, will one day my script stop working and Ill need to login and access the web share again?

Glad you seem to got it working :thumbsup:
As for possible future changes, let hope not ;)
I wouldn't suspect it will ever give you problems, but you never know what will change in the future! I would expect server OS changes to cause more problems than changes in PHP, but always keep what you did in mind in case you do change something.

glish_dreams
12-29-2008, 04:51 AM
noppies it isnt 100% fixed, just clearing all system cache throughout the server removed the 'saved session' into that web folder.


Basicly, if Im inside windows as the (web) user, and attempt to go to the

Fou-Lu
12-29-2008, 07:45 PM
>.<
Doh, I'll have to wait for you to finish that one o.O

glish_dreams
12-30-2008, 12:49 AM
LOL!!!!!! well, I haven't the slightest clue what went wrong on that, ha ha ha.. you know whats even funnier, I still had it open where I left it open from posting, before I hit refresh I read the last thing I posted, it had it all!! ha, odd. oh well. recap...



noppies it isnt 100% fixed, just clearing all system cache throughout the server removed the 'saved session' into that web folder.


Basicly, if Im inside windows as the (web) user, and attempt to go to the [\\SHARE] address, and I am prompted to login. Then Apache/PHP cant open it either.




But to go ahead and reply to myself, I findly found tons of articles with the exact same issue, down to the teeth. Always the correction was to create a new user to run PHP as (which also happens to be the recommended setup for windows). Then setup all the premissions. The little ( I need to validate the user credentials to the [\\SHARE]) Before Apache/PHP can open, is expected. Apparently a batch file can fix that, so I'll give that a whril.
But as far as this forum goes, its pretty much solved. The issue was pretty much Windows the whole time, and I'll also say incomplete Apache configuration.

But thank you for your time, you really helped me along the way big time. Its good to see others with the passion for coding in general , as I hold myself. Maybe I can help you some day ;) HA! good luck with that right

Fou-Lu
12-30-2008, 04:04 AM
NP, you will get it eventually, there is always a way!!!
You'd be surprised how much I don't know (especially when it comes to english :P)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum