Jump to content

subdirectorys


sle09

Recommended Posts

how would i make it also check subdirectorys and use them

 

$dir=opendir($file_dir);

$xml_string= array();

while ($file=readdir($dir))

{

if(substr($file, -3) == "jpg" || substr($file, -4) == "jpeg" ){

$tmp_str = '<image img="'.$file_dir.'/'.$file.'" ';

if($show_titles){

$file_title=substr($file, $omit_chars, strpos($file, ".")-$omit_chars);

$tmp_str .= 'caption="'.$file_title.'" ';

 

}

 

if($link_to_image){

$link_title=substr($file, $omit_chars, strpos($file, ".")-$omit_chars);

$tmp_str .= 'link="'.$file_dir.'/'.$file.'" target="_blank"';

 

}

 

$tmp_str .=" />";

array_push($xml_string, $tmp_str);

}

 

 

}

sort($xml_string);

for($i=0; $i<count($xml_string); $i++){

print $xml_string[$i];

}

closedir($dir);

Link to comment
https://forums.phpfreaks.com/topic/112299-subdirectorys/
Share on other sites

you could turn it into a function like this

<?php
fucntion opendir($file_dir){
$dir=opendir($file_dir);
         $xml_string= array();
            while ($file=readdir($dir))
            {
               if(is_dir($file_dir.'/'.$file)){
                        opendir($file_dir.'/'.$file); // go into subdir
               }
               elseif(substr($file, -3) == "jpg" || substr($file, -4) == "jpeg" ){
               $tmp_str = '<image img="'.$file_dir.'/'.$file.'" ';
               if($show_titles){
                  $file_title=substr($file, $omit_chars, strpos($file, ".")-$omit_chars);
                  $tmp_str .= 'caption="'.$file_title.'" ';
                  
               }
               
               if($link_to_image){
                  $link_title=substr($file, $omit_chars, strpos($file, ".")-$omit_chars);
                  $tmp_str .= 'link="'.$file_dir.'/'.$file.'" target="_blank"';
                  
               }
               
                  $tmp_str .=" />";
                  array_push($xml_string, $tmp_str);
               }
               
            
            }
            sort($xml_string);
            for($i=0; $i<count($xml_string); $i++){
            print $xml_string[$i];
            }
closedir($dir);
}

 

Scott.

Link to comment
https://forums.phpfreaks.com/topic/112299-subdirectorys/#findComment-576557
Share on other sites

While the function below doesn't encompass your own code, it should be easy to see where each part goes. This function uses recursion to go through all subdirectories. The depth parameter means how deep it should go (ie. how many subdirectories it should look in).

 

function viewcontents($path, $depth)
{
if (is_dir($path))
{
	$handle = @opendir($path);

	if (!$handle)
		exit("No permission to access.<br />");

	while (false !== ($file = @readdir($handle)))
	{
		$tfile = str_replace("//", "/", $path."/".$file);

		if (strlen($file) < 3)	continue;				
		if(is_dir($tfile))
		{
			echo "DIR:  ".$tfile."<br />";
			recursiveDelete($tfile, 0);
		}
		else
		{
			echo "FILE: ".$tfile."<br />";
		}
	}
}

if ($depth > 1)
	viewcontents($path, --$depth);
}

Link to comment
https://forums.phpfreaks.com/topic/112299-subdirectorys/#findComment-576655
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.