You can't get to Z: because that drive is not mapped for the local system, only when you loggin. Apache service runs as the local system so it doesn't have any rights outside of the computer it's running on. To "fix" this you can change the system to run as a network user, but make sure that user has system rights on the local computer.
First go into the system services;
1. right click on My Computer and choose Manage.
2. In the window that pops up choose Services and Applications on the left and then choose Services.
3. You should now see Apache in there some where. Open the properties of it and choose the tab Log On.
4. Select a custom account and enter the username/password in the correct boxes. Make sure this user has proper rights both on the computer and other computers.
5. Click Ok and restart the Apache service.
6. Go into your script and change the mapped drive usage to the UNC name. Ex: Z:\dir\file.txt to \Computer\share\dir\file.txt
7. Lucky number seven, give it a try.
Also note I'm guessing that your using Apache as a service and what not. If you use it as a standalone server and you start it up when your logged in, it should work asuming you have proper rights. Hope this helps.