×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Realtive paths

Realtive paths

Realtive paths

(OP)
I am loading a file from a 'same level' directory, relative to the script.
I am more familiar with the syntax of Perl which is simply

CODE

$Menu = "../chargrill.kjw"; 

This doesn't work in PHP and the solutions I have seen ie. realpath or the rather odd looking substr(dirname(__FILE__), strlen($_SERVER['DOCUMENT_ROOT']))
appear far too complicated for a simple problem.

What is the correct syntax?

Keith
www.studiosoft.co.uk

RE: Realtive paths

That would depend on what exactly you are planning on doing with the file.

For example PHP uses include() to bring in a file containing code to be executed.

There's also the fopen(), and fread() functions, used together to read the contents of any file.

But perhaps the more straightforward options is simply file_get_contents() and file().

file_get_contents() reads the contents of a file and puts it in a string, while file does the same but returns an array.

In other words, in PHP you cannot simply name the path of a file and have it be loaded. You need to call a file loading function.


Beyond that, if its in the same path as the script, just use its name, no need for anything else.

CODE

$fileContents = file_get_contents('name_of_file.ext'); 




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Realtive paths

(OP)
Sorry, I wrote the wrong path in my original post.

I am script:-

CODE

http://www.domain/a/b/php/script.php 

which is opening and reading the contents of

CODE

http://www.domain/a/b/menus/file.txt 

The script and file are on the same level but in different directories.
The script works with absolute paths but relative paths don't.
As mentioned previously, in Perl it would be simply, back one directory and then forwards into the menus directory.


Keith
www.studiosoft.co.uk

RE: Realtive paths

To go back one directory in PHP use the double periods.

So:

"../menu/file.txt"

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Realtive paths

(OP)
Thanks Phil.
That is what I started with, just like Perl, but it wouldn't work.
It is working now - very odd.

Keith
www.studiosoft.co.uk

RE: Realtive paths

../chargrill.kjw"

means "One folder level above the current working folder/directory"

Folders or files at the same level use a single dot (./chargrill.kjw) or no './' (chargrill.kjw")

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Realtive paths

(OP)
Chris
I had tried numerous options and ended up, initially posting the wrong one.
I really don't know why it didn't work at first because ../chargrill.kjw was the path I originally tried.
I replaced the 2 periods with the full path and it worked.
I seem to get a lot of caching issues with PHP, is that normal?

Keith
www.studiosoft.co.uk

RE: Realtive paths

Quote:

I replaced the 2 periods with the full path and it worked.

That is known as "root relative" addressing, and is always correct, regardless of where the file is in the structure.

Using "parent path relative" does break if the changes in "current working directory" are not allowed for.

Quote:

I seem to get a lot of caching issues with PHP, is that normal?
On the server the client?

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: Realtive paths

(OP)
I assume the problem is caching.
I call a PHP script and get a page of results.
I edit the script and upload it to the server.
When I call the script again, I get the previous results even though the new code has changed to planned results.
If I add a random char string to the end of the URL, the results are updated.
Is the random char string at the end the only certain way of ensuring up to date results?

Keith
www.studiosoft.co.uk

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close