Jump to content

Path problem


wepnop

Recommended Posts

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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.