wepnop Posted May 17, 2011 Share Posted May 17, 2011 Im having a strange path problem. The problem is that in the base directory, i have some php files. These goes well. I also have two folders: one for my function library, and other for some type of webpages. My problem is that if i changue to a webpage in that folder through the menu in the base directory the path itself it also changues, so it became that directory. Base dir: tr Library directory: lib Category dir: categorias (these two are in tr, and tr in www of wamp) If i echo a link like this in some page that is in the tr directory: <li><a href="categorias/listar_memoria_ram.php">List RAM Memory</a></li> And i go to that web, the include path changues, so it became categorias. I use functions to echo the menu each time, so, it dont work because it uses the relative path, so, if i echo this again: <li><a href="categorias/listar_memoria_ram.php">List RAM Memory</a></li> the url thas is finally called in the web explorer is: http://localhost/tr/categorias/categorias/listar_Memoria_Ram.php That dont exist. Note also that all my web functions are in a file that exists in the lib library and thats included each time using this: include_once '/../lib/lib_inventario.php'; That file have a impMenu function that is used each time, and all the others. What i was seeking if a plain way of controlling the path so it became more simplier and i could just use a single path, without exceptions. Or at least an explanation that why these happens. Ask for whatever any extra information you need. Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/ Share on other sites More sharing options...
spiderwell Posted May 17, 2011 Share Posted May 17, 2011 did you think about using absolute paths? or adding a base path into the html document? Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1216537 Share on other sites More sharing options...
wepnop Posted May 17, 2011 Author Share Posted May 17, 2011 did you think about using absolute paths? or adding a base path into the html document? How and how? Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1216623 Share on other sites More sharing options...
AbraCadaver Posted May 17, 2011 Share Posted May 17, 2011 did you think about using absolute paths? or adding a base path into the html document? How and how? <a href="categorias/listar_memoria_ram.php"> Relative: This states, from whatever the current URL path is, look under it for categorias/listar_memoria_ram.php. So if you are already in /tr/categorias/, it will look for /tr/categorias/categorias/listar_memoria_ram.php. <a href="/tr/categorias/listar_memoria_ram.php"> Absolute: Look for the link as stated from the root of the URL /. It's fairly common practice though to have the main page (the one that loads in the browser) be at the top level in /tr then include() the pages in categorias. Then your relative paths will work because everything is relative to /tr. Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1216640 Share on other sites More sharing options...
wepnop Posted May 18, 2011 Author Share Posted May 18, 2011 There isnt any other way? Because i generate dinamically a lot of php pages and having to include them dinamically sucks. Or maybe i can include all the files in the categories directory using a directory iterator, but that wont be a lot secure i think. What about a system whre i extract the complete path of the application, like: c:/wamp/www/tr And i store it in my configuration object( that is persistent throught sesions). Then i only have to add the needed each time using that string. So if now it uses: echo '<li><a href="categorias/listar_' . $nombre_tratado . '">' . 'Listar ' . $categorias[$entrada]['nombre'] . '</a></li>'; It will use: echo '<li><a href=" '. $complete_path . ' categorias/listar_' . $nombre_tratado . '">' . 'Listar ' . $categorias[$entrada]['nombre'] . '</a></li>'; There is any way of doing this also? Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1217117 Share on other sites More sharing options...
spiderwell Posted May 18, 2011 Share Posted May 18, 2011 you could use it in a common included file with the path variable declared in that file, then if you change the site to a different location you only need to change one file Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1217195 Share on other sites More sharing options...
pornophobic Posted May 18, 2011 Share Posted May 18, 2011 did you think about using absolute paths? or adding a base path into the html document? How and how? <a href="categorias/listar_memoria_ram.php"> Relative: This states, from whatever the current URL path is, look under it for categorias/listar_memoria_ram.php. So if you are already in /tr/categorias/, it will look for /tr/categorias/categorias/listar_memoria_ram.php. <a href="/tr/categorias/listar_memoria_ram.php"> Absolute: Look for the link as stated from the root of the URL /. It's fairly common practice though to have the main page (the one that loads in the browser) be at the top level in /tr then include() the pages in categorias. Then your relative paths will work because everything is relative to /tr. You can use absolute paths in your PHP includes as well, which wouldn't matter what directory level you're in at all. On windows: include_once 'C:/wamp/htdocs/tr/lib/some_lib.php'; On *nix include_once '/path/to/web/root/tr/lib/some_lib.php'; If you want to take the easy way out, you could always you a redirect in an .htaccess file to redirect any requests to /tr/categorias/categorias/* to /tr/categorias/$1. I'm not sure how to do this off the top of my head, but if you wish to take that route to fix links, I suppose I could help you out with it. Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1217238 Share on other sites More sharing options...
wepnop Posted May 19, 2011 Author Share Posted May 19, 2011 I want linux compatibility for that application. My problem now is, i can get the complete path of the application using some php function? What i will do is that the application is created and saved the first time you execute the application in the index page(tr level), and saved in my singleton class of global registry thats its in S_SESSION. So the first time its instancied, it will get the complete path. The problem here i see is that the user may execute first some file in the categorias directory. Then it will get that path as a base. I will check for that as the directory structrue of the application is fixed. Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1217404 Share on other sites More sharing options...
wepnop Posted May 19, 2011 Author Share Posted May 19, 2011 Using realpath(dirname(__FILE__)) i the complete path. But that path is the /lib one that have the library files. There is a easy way of going a up a path level? Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1217409 Share on other sites More sharing options...
wepnop Posted May 19, 2011 Author Share Posted May 19, 2011 Its done. I only have to get if its windows or linux using this, i think: if (strtoupper(PHP_OS)) == 'LINUX') { } else { } str_replace('\lib', '', realpath(dirname(__FILE__))) Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1217412 Share on other sites More sharing options...
wepnop Posted May 19, 2011 Author Share Posted May 19, 2011 if (strtoupper(PHP_OS)) == 'LINUX') { echo 'l'; $this->config['Ruta aplicacion'] = str_replace('/lib', '', realpath(dirname(__FILE__))); } else { echo 'w'; $this->config['Ruta aplicacion'] = str_replace('\lib', '', realpath(dirname(__FILE__))); } That have to work, no? In linux only changue the \ of the path route? Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1217423 Share on other sites More sharing options...
wepnop Posted May 19, 2011 Author Share Posted May 19, 2011 Well. Now i generate a correcte path using these function: function tratar_ruta($ruta) { $path = $_SESSION['reg']->GetConf('Ruta absoluta aplicacion'); if (strtoupper(PHP_OS) == 'LINUX') { } else { $completa = $path . str_replace('/', '\\', $ruta); } return $completa; } It gets a internet path and converts to a absolute file path, for windows, for now. But it dont works. Firefox says that it dont know the protocol for that. Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1217444 Share on other sites More sharing options...
wepnop Posted May 19, 2011 Author Share Posted May 19, 2011 c:\wamp\www\tr\categorias\listar_Ordenador.php this the link generated Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1217466 Share on other sites More sharing options...
wildteen88 Posted May 19, 2011 Share Posted May 19, 2011 You should not be using file system paths for the links in your webpages. Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1217633 Share on other sites More sharing options...
wepnop Posted May 19, 2011 Author Share Posted May 19, 2011 You should not be using file system paths for the links in your webpages. Then how i can work with absolute paths? I need to use then only /tr/? (as it is www/tr) I get it. Absolute path is this: http://localhost/tr/categorias/listar_Ordenador.php But that only works in pure localhost. Bad idea. I think i cant use absolute paths for this, no? Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1217670 Share on other sites More sharing options...
wildteen88 Posted May 19, 2011 Share Posted May 19, 2011 Just start your links with a / <a href="/tr/categorias/listar_Ordenador.php">Blah</a> Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1217681 Share on other sites More sharing options...
wepnop Posted May 20, 2011 Author Share Posted May 20, 2011 Bueh, i solved it, but using some control exceptions. Now i call my menu function with a argument for when its categories or not. Also now i understand the auto-add of urls in html. If you write a / before the url, it adds the base path to it, it was bugging me a lot. Quote Link to comment https://forums.phpfreaks.com/topic/236617-path-problem/#findComment-1217927 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.