Jump to content

Writing a Text File


carriker

Recommended Posts

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[!--quoteo(post=381905:date=Jun 9 2006, 10:49 AM:name=craygo)--][div class=\'quotetop\']QUOTE(craygo @ Jun 9 2006, 10:49 AM) [snapback]381905[/snapback][/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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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.