Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Realtive paths 1

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
GB
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
 
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
 
Sorry, I wrote the wrong path in my original post.

I am script:-
Code:
[URL unfurl="true"]http://www.domain/a/b/php/script.php[/URL]

which is opening and reading the contents of
Code:
[URL unfurl="true"]http://www.domain/a/b/menus/file.txt[/URL]

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
 
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
 
../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
 
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
 
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.

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
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top