Question

I have this script working, which (if it finds something) returns the complete path it finds the file in. I need it to ONLY show the subdirectory it's in.

For instance, if it's found, it will return something similar to:

./prls/carrier1/00000.prl
Already in database!

I wish for it to say just: carrier1.

$dirname = './prls/';
$findme  = $_FILES["file"]["name"];
$dirs    = glob($dirname.'*', GLOB_ONLYDIR);
$files   = array();
//--- search through each folder for the file
//--- append results to $files
foreach( $dirs as $d ) {
    $f = glob( $d .'/'. $findme );
    if( count( $f ) ) {
        $files = array_merge( $files, $f );
    }
}
if( count($files) ) {
    foreach( $files as $f ) {
        echo $f . "<br />";
    }
    echo "Already in database!";
    die;
} else {
    echo "Nothing was found, continue..";//Tell the user nothing was found.
}

How do I only show the subdirectory if a result is returned?

Was it helpful?

Solution 2

Replace your foreach loop with this:

foreach( $files as $f ) {
    $info = pathinfo( $f );
    $parts = explode( DIRECTORY_SEPARATOR, $info['dirname'] );
    $lastSegment = array_pop( $parts );
    echo $lastSegment . "<br />";
}

OTHER TIPS

The pathinfo function may be what you need: http://www.php.net/manual/en/function.pathinfo.php

You can then use the last part of the "dirname" field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top