Jump to content


Photo

Writing a Text File


  • Please log in to reply
6 replies to this topic

#1 carriker

carriker
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 09 June 2006 - 02:45 PM

I'm pretty new to php, so I think this is a pretty simple question. Basically I've got a script that takes in some post data and uses that information to open or create a new txt file, write some text, and save it to a specified location on the hard drive. That part works just fine. The machine that's running php/apache is also on a LAN and I would like to write the same txt file to a different computer on the network. I've tried a couple of times but haven't gotten this to work.

So my first question: is there any reason this can't work? And my second question: if it's possible, what should the file path look like. The file path would normally be "\\OtherComputer\C\TextFiles\SomeTextFile.txt", but I know php likes forward slashes instead of back slashes. However, if I just replace the forward slashes with back slashes it doesn't work.

Thanks in advance,

--carriker

#2 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 02:51 PM

The reason php likes forward slashes instead of backslashes is that the backslash is usually used as an escape character (despite this, Microsoft still decided to use it in their file paths through their infinite wisdom). So if you put it in double quotes like "\\path\to\new\dir" PHP will think the \n is a new line character. A way to solve it would be to put the path in single quotes like '\\path\to\dir' however you said replacing them with forward slashes still didn't work. It should normally as PHP can change them to backslashes if running on Windows which suggests there's something else wrong, I don't know how PHP handles filesystems on other networks. It may have to connect to another server running on the other computer or you may have to assign the other computers drive names like G: and F: and then use those.
[table]



Don't worry, the printer fairies will sort it out.

#3 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 09 June 2006 - 03:49 PM

I have no problem changing the slashes. I run php from my windows box and it seems to interperate

C:\Inetpub\wwwroot AND C:/InetPub/wwwroot As the same thing.

So just use "/" instead of the windows "\" and you should be fine.

Ray

#4 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 03:54 PM

[!--quoteo(post=381905:date=Jun 9 2006, 10:49 AM:name=craygo)--][div class=\'quotetop\']QUOTE(craygo @ Jun 9 2006, 10:49 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I have no problem changing the slashes. I run php from my windows box and it seems to interperate

C:\Inetpub\wwwroot AND C:/InetPub/wwwroot As the same thing.

So just use "/" instead of the windows "\" and you should be fine.

Ray
[/quote]
I did mention that here:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]however you said replacing them with forward slashes still didn't work. It should normally as PHP can change them to backslashes if running on Windows[/quote] and carriker also said he'd tried changing them to forward slashes already here:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]However, if I just replace the forward slashes with back slashes it doesn't work.[/quote]
[table]



Don't worry, the printer fairies will sort it out.

#5 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 09 June 2006 - 04:04 PM

How many PC's do you plan on writing this file to. If you are an admin the only way around writing a file to another computer would be to map a drive on the web server to the remote PC. That way the forward slashes would work no problem. Only problem I see here would be making sure the permission are setup correctly.

I overlooked him trying to connect to a remote computer by using the UNC. My BAD. Using forward slashes from drive letters works no problem.

Ray

#6 carriker

carriker
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 09 June 2006 - 04:38 PM

I did a test with fopen.

if I use fopen with "//OtherComputer/C/test.txt" I get a "Permission Denied" warning.

if I use fopen with "f:/test.txt" where f is the mapped network drive \\OtherComputer\C\ , I get a file does not exist warning.

So it seems like the first option is better, but I'm not sure how to set the appropriate permissions on a windows machine. I've already allowed sharing so anyone on the network to change the files on that drive.

Thanks again,

--carriker

#7 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 05:17 PM

chmod 0777 //OtherComputer/C/ from the command line or the same command in exec()
[table]



Don't worry, the printer fairies will sort it out.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users