spelltwister Posted April 17, 2006 Share Posted April 17, 2006 Hey everyone,I'm having a small problem with writing a file. Pretty much, here's what's happening:I'm running a piece of code that was working fine in one directory, in another and it's giving me problems.The code I'm using is the following:[code]$data = [INSERT XML STUFF HERE]; <-- Obviously this isn't what's there, but it works.$file = "home/online/public_html/game/xml_files/game".$game."turn".$turn."u.xml"; if (!$file_handle = fopen($file,"a")) { echo "Cannot open file"; } if (!fwrite($file_handle, $data)) { echo "Cannot write to file"; } fclose($file_handle);[/code]Ok, the folder this file is in is: home/online/testing and it needs to write to home/online/public_html/game/xml_files.Is there any suggestion on how I can get it to do this. I tried $file as above and as $file = "../public_html/game/xml_files" and still it didn't want to work.Thanks for any help.Mike Quote Link to comment https://forums.phpfreaks.com/topic/7656-writing-a-file/ Share on other sites More sharing options...
poirot Posted April 18, 2006 Share Posted April 18, 2006 Maybe it's a server setting... Some servers just won't let PHP create/delete files in certain or all directories... Quote Link to comment https://forums.phpfreaks.com/topic/7656-writing-a-file/#findComment-27950 Share on other sites More sharing options...
michaellunsford Posted April 18, 2006 Share Posted April 18, 2006 You might try a putting a slash in front of the home directory (assuming it's in root)[code]$data = [INSERT XML STUFF HERE]; <-- Obviously this isn't what's there, but it works.$file = "/home/online/public_html/game/xml_files/game".$game."turn".$turn."u.xml"; if (!$file_handle = fopen($file,"a")) { echo "Cannot open file"; } if (!fwrite($file_handle, $data)) { echo "Cannot write to file"; } fclose($file_handle);[/code]You might also want to experiment with the realpath() function to get exact file locations (especially if you don't know the real or full path to the document).Also, when I get problems like this, I always check permissions of the directory. Sometimes you have to chmod the directory to allow file writing. Quote Link to comment https://forums.phpfreaks.com/topic/7656-writing-a-file/#findComment-27951 Share on other sites More sharing options...
spelltwister Posted April 18, 2006 Author Share Posted April 18, 2006 [!--quoteo(post=365789:date=Apr 17 2006, 08:50 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Apr 17 2006, 08:50 PM) [snapback]365789[/snapback][/div][div class=\'quotemain\'][!--quotec--]Maybe it's a server setting... Some servers just won't let PHP create/delete files in certain or all directories...[/quote]Well, I wrote to that folder from the game folder (IE, home/online/public_html/game wrote to home/online/public_html/game/xml_files (using just $file = "xml_files" etc))If I add the slash won't it look in the current folder for a folder called "home"?How exactly is the realpath() used and what for?ThanksMike Quote Link to comment https://forums.phpfreaks.com/topic/7656-writing-a-file/#findComment-28190 Share on other sites More sharing options...
michaellunsford Posted April 18, 2006 Share Posted April 18, 2006 [!--quoteo(post=366031:date=Apr 18 2006, 10:05 AM:name=spelltwister)--][div class=\'quotetop\']QUOTE(spelltwister @ Apr 18 2006, 10:05 AM) [snapback]366031[/snapback][/div][div class=\'quotemain\'][!--quotec--]If I add the slash won't it look in the current folder for a folder called "home"?[/quote]no, the leading slash will point it to the root directory first. Leaving out a leading slash will look for a folder called "home" in the current directory.[!--quoteo(post=366031:date=Apr 18 2006, 10:05 AM:name=spelltwister)--][div class=\'quotetop\']QUOTE(spelltwister @ Apr 18 2006, 10:05 AM) [snapback]366031[/snapback][/div][div class=\'quotemain\'][!--quotec--]How exactly is the realpath() used and what for?[/quote]Try it out. The following will display the real path to the current directory.[code]<? echo realpath("."); ?>[/code]You could use it for relative path's too, realpath("../../../directory") for example. Quote Link to comment https://forums.phpfreaks.com/topic/7656-writing-a-file/#findComment-28227 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.