php targeting txt file somewhere else on harddrive

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andreas_d
Forum Newbie
Posts: 5
Joined: Sun Jun 20, 2004 5:46 pm

php targeting txt file somewhere else on harddrive

Post 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.?
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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.
andreas_d
Forum Newbie
Posts: 5
Joined: Sun Jun 20, 2004 5:46 pm

Post 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
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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!
Last edited by Illusionist on Sun Jun 20, 2004 6:46 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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");
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

ahh! yes i remember now! I knew something was wrong about what i posted 8O Thanks for catching that
andreas_d
Forum Newbie
Posts: 5
Joined: Sun Jun 20, 2004 5:46 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

from what I remember of mac's.. the directory delimiter was : (colon)
Post Reply