how can I navigate the full path in a php forearch cycle

0

I am trying to walk a foreach that comes by a path get .htaccess, but I'm getting the last element and I need to get the full path. For example:

localhost/file/folder1/folder/2/demo.php

Returns only:

demo.php

This is my PHP code:

$part = array_filter(explode('/', route));
$end_part = end($part);
$file = null;
if(isset($part) && $part[0]=="file" &&  (count($part)>1)){
  unset($part[0]);
  foreach ($part as $value) {
    if($end_part==$value){
      $file = $value;
    }else{
      $file = $value."/";
    }
  }

  echo $file;

}else{
  error_response(404);
}
    
asked by juan cruz 31.12.2018 в 19:37
source

2 answers

0

Php has its own function for the treatment of urls. It's about parse_url. link .

In your case, you can do:

$ list = parse_url ($ url);

With this you will have an associative array with the different parts of your URL. In your case you are interested in the PATH key.

$ path = $ list [ path ];

    
answered by 01.01.2019 / 03:14
source
0

You have to concatenate the value of $ file:

foreach ($part as $value) {
    if($end_part==$value){
      $file .= $value;
    }else{
      $file .= $value."/";
    }
    
answered by 01.01.2019 в 02:55