Page 1 of 1

php targeting txt file somewhere else on harddrive

Posted: Sun Jun 20, 2004 5:46 pm
by andreas_d
hi, i am a bit of a novice when it comes to php, i was hoping someone could help me out. If i want the php script to say, fopen and fwrite into a text file that is in a completely different folder on my computer how could i do it? how would i direct the php script to it.?

Posted: Sun Jun 20, 2004 5:55 pm
by tim

Code: Select all

<?php
fopen("/home/html/tim.txt", "r");

// you can also

$file = fopen("c:\\files\\tim.txt", "r");

// notice the two \\, you must escape the backlash or use the forward slash.

Posted: Sun Jun 20, 2004 6:36 pm
by andreas_d
thanks for the post. that was really quick. I just have a couple of more questions. With the example you gave : fopen("/home/html/tim.txt", "r"); does that mean the txt file is still in a folder within the same folder as the php script? ex. folder 'A' contains: php script and the folder named 'home'.Within that folder is folder 'html' that contains 'tim.txt'. (did that make sense?)
What i want to do is this. Say I have two computers networked and the txt file is on comp1 and the php script is on comp2 how could i direct the php script to the txt file. Again thanks for the help

Posted: Sun Jun 20, 2004 6:44 pm
by Illusionist
To answer your question about the file being on the other computer, i think that you would need the text file in a shared directory, and then you can navigate to that directory and open the text file something like this:

Code: Select all

fopen("COMP2://SharedFiles//MyDir//MyText.txt","r");
//where COMP2 is the name of the second computer.
This isn't tested but, maybe you should test it and see if it works!

Posted: Sun Jun 20, 2004 6:45 pm
by feyd
a leading slash (/) tells to look from the root of the directory structure.. if you want a folder at or below the level of the php script, "some/folders/file.txt" will try to load "file.txt" which is inside the directory "folders" which is inside the directory "some", which in turn is inside the script's directory, or rather the current running directory.

Posted: Sun Jun 20, 2004 6:49 pm
by feyd
I'm fairly sure that won't work Illusionist. You're using protocol structuring..

on windows networks getting to the computer named george is done like so:

"\\george\shared folder\somefile.txt"

so it'd be more like

Code: Select all

fopen("\\\\george\\shared folder\\somefile.txt");

Posted: Sun Jun 20, 2004 6:58 pm
by Illusionist
ahh! yes i remember now! I knew something was wrong about what i posted 8O Thanks for catching that

Posted: Sun Jun 20, 2004 7:15 pm
by andreas_d
I'm actually working with os X so i'm not sure how much it differs from windows in terms of targeting files. So far i have enabled php in my 'server type' folder and have been using the browser to open execute the php scripts with the address

http://pesonalHD.local/~andreas/info/savesunset.php

I'm not sure if that is even relevant but i thought i'd drop it in there anyway

Posted: Sun Jun 20, 2004 7:29 pm
by feyd
from what I remember of mac's.. the directory delimiter was : (colon)