ober Posted August 17, 2010 Share Posted August 17, 2010 I'm trying to create a simple data dump to a text file. It was all working fine and now fopen doesn't like my relative paths. So I'm using an absolute path to the file: C:\\Program Files\\FDS\\DTManage\\Web\\DTM\\php\\DTBill\\sources\\snapshot\\snapshot.txt But fopen keeps saying it can't open the file. It's acting like it isn't there. $path_parts = pathinfo(__FILE__); $filename = str_replace('\\', '\\\\', $path_parts['dirname'])."\\\\snapshot.txt"; if (!$handle = fopen($filename, 'w')) { echo "Cannot open file ($filename)"; } Can someone tell me why this isn't working? Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/ Share on other sites More sharing options...
ober Posted August 17, 2010 Author Share Posted August 17, 2010 By the way, it's a Windows server running IIS. I know... the horror. Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100466 Share on other sites More sharing options...
ober Posted August 18, 2010 Author Share Posted August 18, 2010 Anyone? I'm really lost on this one. Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100688 Share on other sites More sharing options...
HuggieBear Posted August 18, 2010 Share Posted August 18, 2010 Ober, Do you need to escape your backslashes in the str_replace() if they're in single quotes? Personally, I'd replace them with forward slashes. Are you getting any errors output? This should be simple to fix, but I don't have my windows server running at the moment to test. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100698 Share on other sites More sharing options...
ober Posted August 18, 2010 Author Share Posted August 18, 2010 Well the path returned by pathinfo uses backslashes, so I have to use backslashes for the rest unless I replace them all with forward slashes, which I don't think I've tried. And my editor doesn't like it when I don't escape them, so I assumed I had to (PHP Designer 7). This is the error: Warning: fopen(C:\\Program Files\\FDS\\DTManage\\Web\\DTM\\php\\DTBill\\sources\\snapshot\\snapshot.txt): failed to open stream: Permission denied in C:\Program Files\FDS\DTManage\Web\DTM\php\DTBill\sources\snapshot\createExport.php on line 5 Cannot open file (C:\\Program Files\\FDS\\DTManage\\Web\\DTM\\php\\DTBill\\sources\\snapshot\\snapshot.txt) Warning: fclose(): supplied argument is not a valid stream resource in C:\Program Files\FDS\DTManage\Web\DTM\php\DTBill\sources\snapshot\createExport.php on line 32 Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100710 Share on other sites More sharing options...
HuggieBear Posted August 18, 2010 Share Posted August 18, 2010 OK, I have access to my Windows box now. I've just tested your code on a directory with spaces and it works fine. Your error indicates a permissions problem. Check the permissions first, if that doesn't work, use MMC to check that the file handle isn't still open. Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100714 Share on other sites More sharing options...
ober Posted August 18, 2010 Author Share Posted August 18, 2010 The file isn't read only... so what else could it be?? Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100715 Share on other sites More sharing options...
kenrbnsn Posted August 18, 2010 Share Posted August 18, 2010 You're getting a "Permission Denied" error message. You are using Windows Vista which does not allow general users to write files into the "Program Files" directory or any of it's sub-directories. Write your file to a different directory. It always helps to give the exact error message in your initial post. Ken Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100716 Share on other sites More sharing options...
ober Posted August 18, 2010 Author Share Posted August 18, 2010 It's Windows Server 2003. And I'll say it again, this worked when I initially wrote it and the location of the file hasn't changed and the server hasn't changed. And it's not read-only (the file). And the user (as far as the webserver) hasn't changed either. Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100717 Share on other sites More sharing options...
HuggieBear Posted August 18, 2010 Share Posted August 18, 2010 Ober, can you check the file isn't open by using MMC? Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100719 Share on other sites More sharing options...
ober Posted August 18, 2010 Author Share Posted August 18, 2010 Pardon my ignorance... but how do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100722 Share on other sites More sharing options...
HuggieBear Posted August 18, 2010 Share Posted August 18, 2010 Good Question... I'm not sure if it will work on Server 2003. Here's my instructions for XP. Start >> Run Type mmc and click Ok. File >> Add/Remove Snap In >> Add Select 'Computer Management' >> Add >> Finish >> Close >> OK This should now show you the MMC with your local computer. If this works then let me know. Huggie Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100724 Share on other sites More sharing options...
ober Posted August 18, 2010 Author Share Posted August 18, 2010 OK, so that worked... but the file isn't open. I even rebooted the server and it hasn't changed. I guess I'll try writing to another location. Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100731 Share on other sites More sharing options...
HuggieBear Posted August 18, 2010 Share Posted August 18, 2010 OK, so that worked... but the file isn't open. I even rebooted the server and it hasn't changed. I guess I'll try writing to another location. Yeah, start with C:\temp or something. Huggie Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100734 Share on other sites More sharing options...
ober Posted August 18, 2010 Author Share Posted August 18, 2010 I tried in the document_root folder, I tried directly on the C drive... I keep getting the permission denied error. I've even checked the php.ini file to see if those functions were blocked. Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100735 Share on other sites More sharing options...
ober Posted August 18, 2010 Author Share Posted August 18, 2010 OK, I wrote to C:\WINDOWS\Temp and it worked.... WTF. Quote Link to comment https://forums.phpfreaks.com/topic/210995-fopen-with-space-in-windows-path/#findComment-1100740 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.