2014-01-30 68 views
0

我有這個劇本的工作,它(如果發現某事)返回找到文件的完整路徑。我需要它,只顯示它的子目錄。PHP文件搜索響應

舉例來說,如果它的發現,它會返回類似於:

./prls/carrier1/00000.prl
已在數據庫中!

我希望它只是說: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. 
} 

如果返回結果,我該如何顯示子目錄?

+0

問題是什麼?你已經嘗試過了什麼? – kero

回答

-1

這個替換您的foreach循環:

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

謝謝!!!!!!!!!!!! !!!!!!!!!!!!! – user3079125

0

的PATHINFO功能可能是你需要的東西:http://www.php.net/manual/en/function.pathinfo.php

然後,您可以使用「目錄名稱」字段中的最後一部分。

+0

我知道,但因爲輸出給用戶說,這就是他要繼續,如果沒有找到該文件,我想他可能使用文件名之後:-) – Doberman