Jump to content

[SOLVED] HELP: PHP DIRECTORY HANDLING


tonbernradino

Recommended Posts

Hi,

 

I would like to ask how to do this:

 

currently im working in my localhost

 

if I access my localhost the directory in it are:

clientname1

clientname2

 

now, my problem is

 

im in localhost/clientname2

how to get the clientname2 only?

 

 

    $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';

    $URL= $protocol.'://'.$_SERVER['HTTP_HOST']."/";

 

this will display http://localhost/

but I want to include clientname2

 

  $URL= $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

 

it will display

 

  http://localhost/clientname2/pages/product.php?page=about

 

how to display this only http://localhost/clientname2/

 

Link to comment
https://forums.phpfreaks.com/topic/106142-solved-help-php-directory-handling/
Share on other sites

break the directory apart with something like explode, and look for the numbered occurance your interrested in.  If its the directory thats one away from root then:

 

<?php
$directory = dirname($_SERVER["REQUEST_URI"]);
$dir_array = explode("/",$directory);
$lookingfor = $dir_array[1];
?>

function getBaseDirURL()
{
    $url  = (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on')) ? 'https://' : 'http://';
    $url .= $_SERVER['HTTP_HOST'];

    $uri_path = pathinfo($_SERVER['REQUEST_URI'], PATHINFO_DIRNAME);

    // get the base directory
    $pos = strpos($uri_path, '/', 1);

    $url .= ($pos !== false) ? substr($uri_path, 0, ($pos+1)) : $uri_path;

    return $url;
}

echo  getBaseDirURL();

thanks for all your help guys..

 

@phorman

 

yeah it works but my problem is

 

when im in this location

 

http://localhost/clientname2/

 

it displays nothing.

 

but when im in this location http://localhost/clientname2/pages/

 

it display clientname2

 

even im in this location http://localhost/clientname2/ or in this location http://localhost/clientname2/pages/

 

it should display clientname2

 

thanks guys..

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.