The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 12:30:09 PM

Login with username, password and session length


Pages: [1] 2
  Print  
Author Topic: reading files from a different directory with php  (Read 2724 times)
krustee
Rookie
**
Offline Offline

Posts: 33


View Profile
« on: August 24, 2009, 08:07:14 AM »

Decided to post this in the n00b section as it is a n00bish question

I have a  script that reads in a txt file and parses it. Lets say the script is located at ./script/poster/reader.php

The file I want it to read is located in ./script/txtfiles/file.txt

Will this code work in my reader.php script?

Code:
$file = "/txtfiles/file.txt";
$article = read_article($file);

I think I am having the problem because php is looking in ./script/poster/txtfiles how do I get it to go up one directory level?

Do I need to give an absolute path to the file or is there another way to do this?
« Last Edit: August 24, 2009, 08:16:01 AM by krustee » Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #1 on: August 24, 2009, 08:33:46 AM »

When you put a / in front of a path, you are dictating that you want to start from the root of the machine. The ./ is good, but you can also just eliminate the forward slash at the beginning to make it a relative path that will start from where your PHP script was kicked off... that's the reference point to use.

So, file_get_contents('myDir/script/txtfiles') would start at wherever Apache or the shell interpreter had fired off your script[/i/, then drill down into myDir, then script then txtFiles.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
krustee
Rookie
**
Offline Offline

Posts: 33


View Profile
« Reply #2 on: August 24, 2009, 08:43:17 AM »

Thanks for the answer perk but I am quite stupid and still haven't got this figured out.
Ill try to explain myself better

script is at
/script/poster/post.php

and file is at
/script/files/file.txt

So if my script starts looking for the file from where the interpretor kicked off it wont find it as it will look for the file in
/script/poster/files/
instead of
/script/files/

So I think I may just have to use an absolute path to solve this, which leads to another question; If i'm running this from apache on local host would the absolute path be:
/var/www/script/files/
or just
/script/file/
Logged
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #3 on: August 24, 2009, 08:49:33 AM »

it also depends on where your apache user is jailed to i think?

but for pathing.

if your script is here:  /var/www/somedomain.com/script.php

then path to /var/www/textfiles/somefile.txt
relative is: ../textfiles/somefile.txt
absolute is: /var/www/textfiles/somefile.txt

../ means go up 1 dir from where I currently am.

./ means HERE.
so ./../textfiles/somefile.txt is the same thing as ../textfiles/somefile.txt
your script is always ./script.php
often though the ./ is redundant and not needed.
if you leave off the ./ it is assumed you mean HERE if there is no leading slash, so...
./subdir/script.php is the same as subdir/script.php

so from what you are saying about ./script/txtfiles/file.txt
that file.txt should be two directories down from the php script you are running. im guessing its not, but only 1 directory down. ./txtfiles/file.txt or ../txtfiles/file.txt depending on the level of the directory in relation to the script being run.

from what you just posted while i was answering, your path is ../files/file.txt
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
krustee
Rookie
**
Offline Offline

Posts: 33


View Profile
« Reply #4 on: August 24, 2009, 09:06:30 AM »

Thanks nutballs, This solved it for me I think just have to check my script.

The text file is up one directory from the script then down one different directory. I had to make it this way as I have about twenty different scripts that need to access the files in the files directory and I want to keep these scripts in different directories for ease of modification.

the completed code will be like this

Code:
$file = "../txtfiles/file.txt";
$article = read_article($file);
read_article is the reading and parsing function

just checked my script and it works thanks nutballs time for
Logged
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« Reply #5 on: September 08, 2009, 09:37:39 AM »

So, file_get_contents('myDir/script/txtfiles') would start at wherever Apache or the shell interpreter had fired off your script[/i/, then drill down into myDir, then script then txtFiles.

How can you tell where the shell interpreter has fired off your script *THIS TIME* ?


Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #6 on: September 08, 2009, 09:41:29 AM »

$startupDir $_SERVER['DOCUMENT_ROOT']

Lots of good stuff here:
http://us3.php.net/manual/en/reserved.variables.server.php
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« Reply #7 on: September 08, 2009, 09:45:34 AM »

awesome. thanks!   Idea...  good stuff.
Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
jammaster82
Lifer
*****
Offline Offline

Posts: 666


Thats craigs list for ya


View Profile
« Reply #8 on: September 08, 2009, 10:07:04 AM »

Okay thats the TOP directory... where the script Starts
from when i try to fopen for example is located at:

$_SERVER['SCRIPT_FILENAME']

And im getting back something like:

/blah/blah/htdocs/myprojectdirectory//wp=admin/options-general.php

And this is where i back out of with a ../ and get to another
dir and then drill down..  my question is, what is this: 

//

Logged

The watched pot, never boils... But if you walk away from it , the soup burns.  What gives?
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #9 on: September 08, 2009, 10:10:35 AM »

The machine is ignoring the double and just thinking '/' - it means that between wordpress and Apache they added a double slash to the URL. No worries.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #10 on: September 08, 2009, 10:38:58 AM »

Documentroot seems to not be very consistent from server to server in my experience. Some servers it seems to point to a directory that is NOT where the stuff actually is.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #11 on: September 08, 2009, 11:45:46 AM »

Hmm... under what circumstances? You're right that SCRIPT_FILENAME is probably a better bet.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #12 on: September 08, 2009, 03:50:49 PM »

I actually cant figure it out. But even on MY server, the Documentroot is wrong.
Documentroot returns what is set in my httpd.conf file. But that is not right. It seems to return the value of the default document root, but not the individual host header site document root.
Unless I have something set wrong, which is entirely possible.

that reminds me... I need to install your server. LOL
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #13 on: September 08, 2009, 04:13:41 PM »

Holy smokes, just remembered that. Howz Thurs?
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #14 on: September 08, 2009, 05:36:27 PM »

lol. er. I think thursday is fine. Assume it is. not sure if we are having our weekly corp meeting or not, which would dictate the time since its always in the morning.
God we suck. Our last corp meeting that we actually had was like 6 weeks ago. supposed to be weekly...
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
Pages: [1] 2
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!