Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php recursive search problem

Options
  • 15-07-2005 10:23am
    #1
    Registered Users Posts: 180 ✭✭


    Hi there,

    I was wondering if someone could help me with a problem that I am having.

    What I am trying to do is to search through a range of directories and sub-directories for images that contain certain characters in their name.

    Here's what I have done so far.

    [php]
    function create_tree($file_dir,$search_text) {
    if ($handle = @opendir($file_dir)) {
    $i=0;
    while (false !== ($file = @readdir($handle))) {
    if ($file != "." && $file != "..") {
    $list[$i]=$file;
    $i++;
    }
    }

    $dir_length = count($list);

    $j = 0;
    for($i=0;$i<$dir_length;$i++) {

    if(strrpos($list[$i], ".") == FALSE) {
    create_tree($file_dir."/".$list[$i],$search_text);
    } else {
    $file_list[] = $file_dir."/".$list[$i];

    ////////////////////////////////////////////////////////////////////////////////
    // check that the file is a picture
    $ext = getExtension($file_list[$i]);
    if ( $ext === "JPG" || $ext === "jpg") {
    if ($currdir === "thumbs") {
    echo "<br>" . $currdir . "is a thumbnail directory";
    }
    ////////////////////////////////////////////////////////////////////////////////
    // check for a match with our search string
    if (preg_match("/$search_text/","$list[$i]")) {
    //echo "<br> Search ".$search_text." and ".$list[$i];
    $f_list[$j] = $file_dir . "/" . $list[$i];
    echo "<br>" . $f_list[$j];
    echo "<br> WE HAVE A MATCH with ".$search_text." and ".$list[$i];
    $j++;
    }
    else {
    $match = false;
    }
    }

    }
    }
    closedir($handle);
    }
    return $f_list;
    }

    $listoffiles = create_tree($currdir,$search_text);
    [/php]


    As you can see I am calling the function with the current directory and the search text as the parameters. The function should then build up an array of images with their relevant paths and return this as a list.

    What happens is the images are found ok and the full path to the image is displayed but nothing gets returned.

    If I could just get that array to return I would be home and dry but for the life of me I cannot figure out what the problem is.

    Maybe I am missing something obvious but it has confounded me.

    Any help would be greatly appreciated.


Comments

  • Registered Users Posts: 2,243 ✭✭✭zoro


    i had to write a custom recursive function for determinng file sizes and directory sizes recently - i'll grab teh code for you later on. i can remember it being an absolute nightmare :)


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    Worked for me. I just have to replace getExtension (and following line) with [PHP]if ( preg_match( "/\.jpg$/i", $file_list[$i] )) {[/PHP]
    I also appended:[PHP]echo '<b>$listoffiles</b><pre>'; print_r( $listoffiles ); echo '</pre>';[/PHP] in order to see the returned data.

    When I called the function: [PHP]$listoffiles = create_tree('/var/www/icons','s');[/PHP] it displayed:
    /var/www/icons/screw1.png
    WE HAVE A MATCH with s and screw1.png
    /var/www/icons/screw2.png
    WE HAVE A MATCH with s and screw2.png
    /var/www/icons/script.png
    WE HAVE A MATCH with s and script.png
    /var/www/icons/small/compressed.png
    WE HAVE A MATCH with s and compressed.png
    /var/www/icons/small/burst.png
    WE HAVE A MATCH with s and burst.png
    /var/www/icons/small/sound2.png
    WE HAVE A MATCH with s and sound2.png
    /var/www/icons/small/sound.png
    WE HAVE A MATCH with s and sound.png
    /var/www/icons/small/transfer.png
    WE HAVE A MATCH with s and transfer.png
    /var/www/icons/small/ps.png
    WE HAVE A MATCH with s and ps.png
    
    $listoffiles
    Array
    (
        [0] => /var/www/icons/screw1.png
        [1] => /var/www/icons/screw2.png
        [2] => /var/www/icons/script.png
    )
    

    Side note: when my search string was '.' it didn't match everything!


  • Registered Users Posts: 180 ✭✭marcphisto


    Thanks for the effort daymobrew but that didn't work at all for me. It found no matches what soever. I suspect this is to do with me handing it "." as the $currdir.

    When I handed it parameters such as you suggested it did find matches but still failed to return the variable.

    I am genuinely at alost as to why it is not returning anything.
    :confused:


  • Registered Users Posts: 2,243 ✭✭✭zoro


    yeah i had that problem too ... and i forgot to get you the code :)

    I'll get it later on :p


  • Registered Users Posts: 2,243 ✭✭✭zoro


    Try this out ... if you've any questions, feel free to post them up.
    It took me quite a while to perfect this, and I know that it could be cleaned up a good bit, but it works :)
    <?
    //NECCESSARY LINES
    $downloaddir = "/var/tmp/zoro/not/a/real/folder/";
    Extract($_GET);
    ?>
    
    <DIV class=contentHeader>
    <H1>Download Section</H1>
    </DIV>
    <div class=description>
    Welcome to the downloads section</div>
    
    <?php
    if (!isset($dir))
       $dir = "";
    
    $originaldir = $dir;
    $dir = $downloaddir.$dir;
    
    function listfiles($dirtocount, $countOnly = false)
    {
    	global $originaldir, $dir;
    	$handle=opendir($dirtocount);
    	$dldir=array();
    	$dlfile=array();
    	$dircount=0;
    	$filecount=0;
    	while (($file = readdir($handle))!==false)
    	{
    		if ($file == "." || $file == "..")
    			$file = $file;
    		elseif(!is_dir($dirtocount."/".$file))
    		{
    			if($countOnly !== true)
    			{
    				$dlfile[$filecount][0] = $file;
    				$sizeoffile = filesize($dirtocount."/".$file);
    				$sizes = array(" B", " KB", " MB", " GB");
    				$megs = array(1, 1024, 1048576, 1073741824);
    				$sizeIndex = 0;
    
    				if($sizeoffile >= $megs[$sizeIndex + 1])
    				{
    					$sizeIndex++;
    					if($sizeoffile >= $megs[$sizeIndex + 1])
    					{
    						$sizeIndex++;
    						if($sizeoffile >= $megs[$sizeIndex + 1])
    							$sizeIndex++;
    					}
    				}
    				$sizeoffile = round(($sizeoffile/$megs[$sizeIndex]), 2)." ".$sizes[$sizeIndex];//." -&gt ".$sizeoffile;
    				$dlfile[$filecount][1] = $sizeoffile;
    			}
    			$filecount++;
    		}
    		else
    		{
    			if($countOnly == true)
    				$filecount = $filecount + listfiles($dirtocount."/".$file, true);
    			else
    			{
    				$filec = listfiles($dirtocount."/".$file, true);
    				$dldir[$dircount][1] = " - (".$filec.")";
    			}
    			$dldir[$dircount][0] = $file;
    			$dircount++;
    		}
    	}
    	closedir($handle);
    
    	if($countOnly == true)
    		return $filecount;
    
    	sort($dldir);
    	sort($dlfile);
    	$counter = 0;
    	$dldirName = "";
    	$dlfileName = "";
    
    	if (sizeof($dldir) > 0)
    	{
    		echo"<br><Br><font color=red><b>Download Sections:</b></font><br>";
    
    		for ($f = 0; $f < sizeof($dldir); $f++)
    		{
    			$dldirName = $dldir[$f][0];
    			$filec = $dldir[$f][1];
    			$ScriptName = $_SERVER['SCRIPT_NAME'];
    			echo "<b><a href=\"$ScriptName?dir=".$originaldir."/".$dldirName."\">".$dldirName."</a>".$filec."</b><br>";
    		}
    		echo "<br>";
    	}
    	else
    		echo"<br><Br><font color=red><b>No directories</b></font><br>";
    
    	if(sizeof($dlfile) > 0)
    	{
    		echo"<br><Br><font color=red><b>List of files in this section:</b></font><br>";
    		echo "<font color=navy><b><em>You may have to right click on your download and select \"Save as\" to download these files correctly</em></b></font><br>";
    
    		for ($f = 0; $f < sizeof($dlfile); $f++)
    		{
    			$dlfileName = $dlfile[$f][0];
    			$dlfileSize = $dlfile[$f][1];
    			$ScriptName = $_SERVER['SCRIPT_NAME'];
    			echo "<a href=\"".$dir."/".$dlfileName."\">".$dlfileName."</a> - <strong>(".$dlfileSize.")</strong><br>";
    		}
    	}
    	else
    		echo"<br><Br><font color=red><b>No files</b></font><br>";
    
    	echo"<BR><BR>";
    }
    
    
    if(isset($dir))
    {
    	if(isset($file))
    		echo "<p>You have chosen to download $file.<br><br>Click <a href=\"$dir/$file\">here</a> to start download";
    	else
    	{
    		if (strstr($dir, "../") > -1)
    			echo"You have no business in this directory";
    		else
    		{
    			if(is_file($dir))
    				echo"<center><font size=+1><b><a href=\"".$dir."\">..Download ".$dir."..</a></center>";
    			else
    				listfiles($dir);
    		}
    	}
    }
    else
    	echo "<P>Welcome to the Download section!";
    ?>
    


  • Advertisement
  • Registered Users Posts: 2,243 ✭✭✭zoro


    by the way, this doesnt do what you want, but it's a start :)
    the last thing i'd wanna do is give you a complete solution :p


  • Registered Users Posts: 95 ✭✭fractal


    Seems a bit over-complicated the way you're doing it.. I'd say this function will do what you want unless I completely missed th epoint..
    <?php
    $everything = search_dir("/home/fractal/public_html/","jpg$");
    
    function search_dir($dirpath,$regex)
    {
            $stack[] = array();
    
            if (is_dir($dirpath))
            {
                    if ($dh = opendir($dirpath))
                    {
                            while (($file = readdir($dh)) !== false)                // cycle through directory
                            {
                                    if(filetype($dirpath.$file) == "dir" && $file != ".." && $file != ".")
                                    {
                                            $stack = array_merge($stack, search_dir($dirpath.$file."/",$regex));
                                    }
                                    else if(eregi($regex,$file))
                                    {
                                            array_push($stack,$dirpath.$file);
                                    }
                            }
                            closedir($dh);
                    }
                    else
                    {
                            echo "INVALID DIRECTORY";
                    }
            }
    
            return($stack);
    }
    ?>
    


  • Registered Users Posts: 180 ✭✭marcphisto


    Wayhay! Got it going in the end.

    for those that are interested

    [php]
    $f_list[] = array();
    ////////////////////////////////////////////////////////////
    // Create a hierarchical tree of the sub directories (recursive)
    function create_tree($file_dir,$search_text,$j) {
    //$f_list[] = array();
    global $f_list;
    if ($handle = @opendir($file_dir)) {
    $i=0;
    while (false !== ($file = @readdir($handle))) {
    if ($file != "." && $file != "..") {
    $list[$i]=$file;
    $i++;
    }
    }

    $dir_length = count($list);

    //$j = 0;
    for($i=0;$i<$dir_length;$i++) {

    if(strrpos($list[$i], ".") == FALSE) {
    create_tree($file_dir."/".$list[$i],$search_text,$j);
    } else {
    $file_list[] = $file_dir."/".$list[$i];

    ////////////////////////////////////////////////////////////////////////////////
    // check to see if it is a picture
    $ext = getExtension($list[$i]);
    if ($ext === "jpg" || $ext === "JPG") {
    ////////////////////////////////////////////////////////////////////////////////
    // check for a match with our search string
    if (preg_match("/$search_text/i","$list[$i]")) {
    // if (preg_match("/$search_text\.jpg$/i", $file_list[$i] )) {
    //echo "<br> Search ".$search_text." and ".$list[$i];
    //$f_list[$j] = $file_dir . "/" . $list[$i];
    array_push($f_list,$file_dir . "/" . $list[$i]);
    //echo "<br>$j " . $f_list[$j];
    //echo "<br> WE HAVE A MATCH with ".$search_text." and ".$list[$i];
    $j++;
    }

    }

    }
    //return $f_list;
    }
    closedir($handle);
    }
    //return ($f_list);
    }

    create_tree($currdir,$search_text,0);
    [/php]

    I will be doing a bit of cleaning up of the code but this does the trick if you need to search through all directories and subdirectories for image files.

    with a bit of modification it'll work for any/all file types

    thanks for the help :D


Advertisement