Jump to content

Archived

This topic is now archived and is closed to further replies.

carriker

Writing a Text File

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

Share this post


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

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
chmod 0777 //OtherComputer/C/ from the command line or the same command in exec()

Share this post


Link to post
Share on other sites

×

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.