Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/7656-writing-a-file/
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/7656-writing-a-file/#findComment-27951
Share on other sites

[!--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?

Thanks

Mike
Link to comment
https://forums.phpfreaks.com/topic/7656-writing-a-file/#findComment-28190
Share on other sites

[!--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.
Link to comment
https://forums.phpfreaks.com/topic/7656-writing-a-file/#findComment-28227
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.