php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #20875 function to fix path names
Submitted: 2002-12-07 04:26 UTC Modified: 2015-04-02 02:07 UTC
Votes:5
Avg. Score:4.2 ± 1.2
Reproduced:4 of 4 (100.0%)
Same Version:1 (25.0%)
Same OS:1 (25.0%)
From: briantmeyer at earthlink dot com Assigned: kalle (profile)
Status: Closed Package: *General Issues
PHP Version: 4.3.0RC2 OS: win2000
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: briantmeyer at earthlink dot com
New email:
PHP Version: OS:

 

 [2002-12-07 04:26 UTC] briantmeyer at earthlink dot com
It would be nice to have a generic function to clean up 
paths so they will work on all platforms. Similar in scope 
to addslashes but focusing on the issues of paths and cross 
platform compatibility of scripts.

Researhing this i only found http://bugs.php.net/
bug.php?id=10916 was related but that was from a long time 
ago. There also seems like there are equivalents in other 
languages (VMS, PERL)

I know there is already much code for this out there, 
because everyone that writes cross platform has to have a 
solution, and that all of it is fairly simple to create as 
needed if you pay attention to it revolving mainly around 
"/" changing into "\" and vice versa.

However, being relatively new to php, it seems that this 
type of code is overly prevalent and would be the perfect 
use for a function, where you pass it the wrong unix/
windows path and it returns the path for the platform you 
are installed on, or even semi-intelligently figure out 
what you really meant. 

It should be added to the core distribution, due to it's 
purpose to allow greater collaboration between coders 
across different installations, and the extent of use in a 
single application would not warrant using a function in 
most cases. 

Isn't it difficult to adequately debug for developers that 
are only on a single platform without such a tool? They'll 
have no idea until they test it. Code related to this issue 
is in all the opensource projects, but only in one or two 
places per project and was probably only added upon someone 
trying to use on the wrong platform.

It also seems like this could allow the php language to 
elegantly handle variations with other separators such as 
the older mac ":". For instance, XP changed the hard drive 
letter from the traditional c:\ to d:\ or whatever the last 
drive is. I am sure that requires some script tweaking. As 
changes occur, they can happen globally across the entire 
php community allowing older scripts that use this to keep 
on working.

This would make code generally more cross platform without 
the necessary "if platform, then this path" that needs to 
be tested and debugged on both platforms. It is, as i am 
thinking of it, a very simple thing needed more for 
compatibility between installs than anything else, It seems 
like this is the only real area of code difference between 
unix and windows machines and working solely with the 
windows platform, i already know my scripts will break on 
the other platforms with my use of "\\". Adoption of a 
function as such would allow me to develop on windows, and 
deploy (or share) on unix without as many issues(or vice 
versa). It also would allow me to use "/" as instead of "\\
" in my paths which seem to look more "right" to me and 
would make my scripts the same as the unix/mac/linux 
developer. (It gets hard to keep track of which are escapes 
and which are slashes sometimes). It also might allow the 
distribution php.ini files to use cross platform paths that 
work asis on both platforms if all paths are run through 
this function as they are read from this file.

Having a prepend option that would allow easy access to get 
the path of folders that change from machine to machine 
without extra coding. This has always been a great feature 
of applescript and should find it's way into php. The web 
root folder, include folders, OS directory, php directory, 
or paths set in php.ini. Perhaps even allow developers to 
add paths to php.ini that configure their products.



fixpath($path, [prepend], [append], [unix/pc/html 
override])

fixpath($path, "c:/", "", "PC")   // as opposed to "c:\\" 
makes easier to read

fixpath($path, "INCLUDEFOLDER", "", "PC")       // use of 
keyword and returns windows path



Basically it would

a. determine platform

b. convert path "/" to "\" or "\" to "/" b
ased on platform, if wrong

c. perhaps add a c:\ or other
 path information to the beginning as needed

c. re
move any "//" due to  
        
$
rootdir = "/bin/";
        $thisdir = $rootdir."/somedir/
";   // or other platform restrictions
   (yes i know, dumb 
mistake but still nice to elegantly fix)

d. test to se
e if file exists, perhaps looking through include fol
ders

e. have a prepend/append string

f. override path wi
th platform

g. ex
p
and strings like .. to enclosing folder path

h. fix invali
d characters on platform --- (target file after file upload)


i.  have keywords that convert to local system paths ww
root/currentpath/domain root (http://www.url.com/)/or p
hp.ini settings added by user. 

j.  resolve an Alias/S
hortcut. 

and would return a valid absolute path to a fi
le




This seems like a very easy and fast thing to code fo
r the most part, even for me with m
y
 
current limited kno
w
ledge of php. I also w
o
uld make the languge better, easier, and more elegant.

How
 
would something like this get into the core dis
t
ribution?
What would keyword
s be, what needs to be access
ed? Is there already an accepted Syntax 
that should be used for keywords this function accesses.
Are there other path related platform issues?
Are there folder paths that are hard to access?
Is the code for the functions in the core distribution 
written in php or in a higher level language?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-04-02 02:07 UTC] kalle@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: kalle
 [2015-04-02 02:07 UTC] kalle@php.net
We got realpath() nowadays, although this is costs a bit but it pretty much does what you suggest.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 10:01:29 2024 UTC