diff --git a/inc/search.php b/inc/search.php
index dd5bda9be773b6a3bd765dcf2bad41e25f76e1a0..2462cb2408f2839b896f3ddc55bb1c2e58c924f0 100644
--- a/inc/search.php
+++ b/inc/search.php
@@ -188,7 +188,7 @@ function search_media(&$data,$base,$file,$type,$lvl,$opts){
     //we do nothing with directories
     if($type == 'd') {
         if(!$opts['depth']) return true; // recurse forever
-        $parts = explode('/',$file);
+        $parts = explode('/',ltrim($file,'/'));
         if(count($parts) == $opts['depth']) return false; // depth reached
         return true;
         return ($opts['recursive']);
@@ -293,7 +293,7 @@ function search_allpages(&$data,$base,$file,$type,$lvl,$opts){
     //we do nothing with directories
     if($type == 'd'){
         if(!$opts['depth']) return true; // recurse forever
-        $parts = explode('/',$file);
+        $parts = explode('/',ltrim($file,'/'));
         if(count($parts) == $opts['depth']) return false; // depth reached
         return true;
     }