Skip to content
Advertisement

How to get directory name from file path using php

I am trying to get dir name from my file path: Currently below is my output:

Array
(
    [path] => documentLibraryworkmarketingProcessBusinessMarketingDesignImages2_Product_Images4_SHORT-RANGENINA1_NINA-B1source_WEB
)

and I want to get second last name which is (01_NINA-B1). What I am trying is:

 echo "<pre>".print_r(dirname($results),true)."</pre>"; die;

when I add dirname above it displays nothing. What can I try next?

     $query = db_select('network_drive','networkd');
     $query
    ->fields('networkd', array('path'))
    ->condition('ndid',$networkdriveid,'=')
    ->orderBy('networkd.ndid');
    $results = $query->execute();
    echo "<pre>".print_r(dirname($results['path']),true)."</pre>"; die;

Advertisement

Answer

Is this drupal? from my research I found out that db_select is a drupal function.

Can you try this one? it will attempt to explode the path string and find the second last element, if none is found it will return “N/A”

 $query = db_select('network_drive','networkd');
 $query
->fields('networkd', array('path'))
->condition('ndid',$networkdriveid,'=')
->orderBy('networkd.ndid');
$results = $query->execute();
foreach($results as $result){ 
    $array = explode("\", $result->path);
    echo isset($array[count($array) - 2]) ? $array[count($array) - 2] : "N/A";
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement