slash problems using Linux and Windows

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
mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

slash problems using Linux and Windows

Post by mcog_esteban »

hi all.
can some explain this to me:
i got a proj from a guy that was working win linux, and when i was testing i got problems (not showing images,bad files includes,etc...), when i got the code i had to change every link like /images/aaa.gif to images/aaa.gif to see everything working well.

Can someone tell me why do i got this problem with the slashes?
There's any simple fix to resolve this?
Can i use the same code(with slahes) in both OS's?

Thanks.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Yes, you can use forward slashes on both, eg /images/foo.gif
I use this same style on both windows and *nix (both running apache, i'm not sure if that style would be a problem on IIS .. it shouldn't make any difference though).
mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

Post by mcog_esteban »

but what i was telling that i had to delete the slahes to get it work on Windows.

Therefore using both slashes will not my problem.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

The slashes won't be the problem directly as forward slashes work fine on windows.
What's the exact format that produces the problem ?
When using forward slashes what output do you get (the html source) ?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Well if the slashes are in the html I can't really see that as being a problem.
mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

Post by mcog_esteban »

nevermind...it's working now(with the slashes).
thank you both.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

first of all this has nothing to do w/ what os or anything, it has to do w/ filesystem structure and the document root.

ok, lets say your testing on your local machine.

your machines doc root is accessed by typing http://localhost into the browser

when you have a file path hich BEGINS w/ a slash, you are specifying a filepath relative to a root directory

lets say your html page is on
http://example.com/testing/foo/bar.html
now in bar.html, if you link to images like this
src="images/apple.gif"
the browser will look for a file at
http://example.com/testing/foo/images/apple.gif
BUT if you link like this
src="/images/apple.gif"
the browser will look for the file at
http://example.com/images/apple.gif

the leading slash means relative to the root



same goes for filesystem operations in php or anything else

if your script is at
C:/foo/bar/your_doc_root/index.php
and in index.php you call
include '/script.php';
you are calling the file
C:/script.php

again, the leading slash makes it relative to the root directory
this is same on all OS as far as i know.

so the person who was working before you just had his filesystem structure, and his webservers document root different than yours.



me personally, i ALWAYS write images paths with a leading /. that way no matter what url the user is on in my site, it works

same goes for includes in php, i make my own "root" like so

Code: Select all

include $_SERVER['DOCUMENT_ROOT'] . '/path/to/script.php';
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

some handy php constants:

Code: Select all

<?
echo PHP_OS;
echo DIRECTORY_SEPARATOR;
?>
Post Reply