chronister Posted January 23, 2009 Share Posted January 23, 2009 Hello, I have a class that I am writing. This class creates some meta links (css, js files) that are placed in the header. I have a config file to define the relative path the class directory is placed in, but wish to have it figure this out dynamically. My current structure is like so.... site root |- class dir |- library dir |- config.php I would like config.php to determine that class dir is located at /classDir/ .. and if the structure changes e.g. site root |-subDir |- classdir |- libDir |- config.php then it determines that the class is in /subDir/classDir/ As I mentioned, the files are css & js files, and I would like them to be linked/referenced as /path/to/class/stuff/ so I can place the class dir anywhere, and it figures out where it lives. I am pretty sure this is possible, but I can't quite get it right. I used $_SERVER['PATH_TRANSLATED'], and it worked when calling the class inside a file that lives in the same dir, but when I call the class from a file in site root, it is not working.. I understand why.. but cannot quite get this one down. I also tried dir_name(__FILE__) type methods, but they gave me the full server path e.g. c:\program files\....... (windows test machine) Any takers on helping with this?? Thanks, nate Link to comment https://forums.phpfreaks.com/topic/142086-dynamically-determine-the-dir-a-file-is-in/ Share on other sites More sharing options...
uniflare Posted January 23, 2009 Share Posted January 23, 2009 I dont *quite* understasnd what you specifically want but try: $_SERVER['PHP_SELF']; Link to comment https://forums.phpfreaks.com/topic/142086-dynamically-determine-the-dir-a-file-is-in/#findComment-744115 Share on other sites More sharing options...
chronister Posted January 23, 2009 Author Share Posted January 23, 2009 I tried that.... It gives me the C:\Program Files\apache ..... path.... I need to dynamically determine the path to use to call js/css files.... e.g. <script src="/path/to/jsFile.js" ></script> or <link href="/path/to/css/file.css" rel="stylesheet" /> I am not sure if it is possible to do this without some serious coding.. but I would like to find out. Nate Link to comment https://forums.phpfreaks.com/topic/142086-dynamically-determine-the-dir-a-file-is-in/#findComment-744124 Share on other sites More sharing options...
uniflare Posted January 23, 2009 Share Posted January 23, 2009 php self should give a relative path, not a direct path. im not sure why it would give you the drive etc. to get rid of the scriptname off the end you could use preg_match(); or preg_replace(); Link to comment https://forums.phpfreaks.com/topic/142086-dynamically-determine-the-dir-a-file-is-in/#findComment-744125 Share on other sites More sharing options...
uniflare Posted January 23, 2009 Share Posted January 23, 2009 you can always get rid of the document root part with a simple str_replace($_SERVER['DOCUMENT_ROOT'],"",__DIR__); Link to comment https://forums.phpfreaks.com/topic/142086-dynamically-determine-the-dir-a-file-is-in/#findComment-744127 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.