Jump to content

how to get php to scan all subdirectories


natturefrk

Recommended Posts

I have been trying to figure this out for about a week now and cannot figure it out for the life of me. I have a script that displays images from a folder and I can limit the images displayed by what ever number I choose that is what $per_row is for. It has an more images link at the bottom of the div that feeds a function the id of the group to display the rest of the images. What I am trying to figure out is how to get php to scan all the directories and have a seperate div for each directory where the more images link will still feed the right id to the function.

 

Here is what I got so far:

 

 
<?php

echo '<link rel="stylesheet" type="text/css" href="../css/normalize.css" />';

echo '<link rel="stylesheet" type="text/css" href="../css/sizing.css" />';

echo '<link rel="stylesheet" type="text/css" href="../css/images.css" />';

include '../includes/connect.php';

include '../includes/vars/images.var.php';

include '../includes/functions/images.func.php';

$group_id = grab_group_id2($db, $album_name);

$dir = 'upload/hello/';

$per_row = 16;

if(file_exists($dir) == FALSE){
    
    echo 'Sorry, but the folder' . $dir . ' does not exist!';
    
}else{
    
    $dir_contents = scandir($dir);
    
    echo '<div class="images_container">';
    
    foreach ($dir_contents as $file) {
        
        $explode_image_filename = explode('.', $file);
        
        $file_type = strtolower(end($explode_image_filename));
        
        if($file !== '.' && $file !== '..' && in_array($file_type, $ext) == TRUE){
            
            if($counter % $per_row === 0 && $counter !== 0){
                        
                break;
                
            }
            
              echo '<a href="' . $dir . '/' . $file . '" alt="' . $file . '"><img src="' . $dir . '/thumbs/thumb_' . $file . '" alt="' . $file . '" class="images" /></a>';
                
                $counter++;             
                
        }
        
    }
            
            echo '<br /><a href="more_images.php?id='.$group_id.'" class="more-images-link">More Images...</a>';
}
        

?>
 

Link to comment
Share on other sites

ok I got all the paths but how do I display the images in there own divs while limiting how much is displayed in those divs and still be able to us the more images link. I am really sorry for the noob question, but I have 0% knowledge about PHP OOP. Is there a way to do this with procedural coding?

 

This is what I got so far

 

$startpath = 'upload/';

$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($startpath, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
$r = array();
foreach ($ritit as $splFileInfo) {
   $path = $splFileInfo->isDir()
         ? array($splFileInfo->getFilename() => array())
         : array($splFileInfo->getFilename());

   for ($depth = $ritit->getDepth() - 1; $depth >= 0; $depth--) {
       $path = array($ritit->getSubIterator($depth)->current()->getFilename() => $path);
   }
   $r = array_merge_recursive($r, $path);
 
}

echo '<pre>';

print_r($r);

echo '</pre>';

Edited by natturefrk
Link to comment
Share on other sites

I added wrong code above and can't edit it. Just realized it though.

 

$path = 'upload/';

/* @var $file SplFileInfo */
foreach (new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($path)) as $file) {
    if ($file->isFile()
        && in_array(strtolower($file->getExtension()), array('jpg', 'png', 'gif'))) {
        $all_paths = $file->getPathname()."<br>";
    
        //echo '<img src="'.$all_paths.'" />';
        
    }

}

 

When I echo $all_paths this is the output. So I am getting the paths. I tried to put $all_paths in the src attribute as you can see above but it just returns broken images.

 

upload\Google\egret.jpg
upload\Google\GraffitiCharactersTheCangolier.jpg
upload\Google\hammers.png
upload\Google\pictures.png
upload\Google\thumbs\thumb_egret.jpg
upload\Google\thumbs\thumb_GraffitiCharactersTheCangolier.jpg
upload\Google\thumbs\thumb_hammers.png
upload\Google\thumbs\thumb_pictures.png
upload\Google\thumbs\thumb_turntable.jpg
upload\Google\turntable.jpg
upload\Test\aboutpage.JPG
upload\Test\contactpage.JPG
upload\Test\csscert.gif
upload\Test\djgraffiti.jpg
upload\Test\eggs.jpg
upload\Test\filmstrip.jpg
upload\Test\fundingpage.JPG
upload\Test\homepage.JPG
upload\Test\parrot.jpg
upload\Test\thumbs\thumb_aboutpage.JPG
upload\Test\thumbs\thumb_contactpage.JPG
upload\Test\thumbs\thumb_csscert.gif
upload\Test\thumbs\thumb_djgraffiti.jpg
upload\Test\thumbs\thumb_eggs.jpg
upload\Test\thumbs\thumb_filmstrip.jpg
upload\Test\thumbs\thumb_fundingpage.JPG
upload\Test\thumbs\thumb_graffitiwall2.jpg
upload\Test\thumbs\thumb_homepage.JPG
upload\Test\thumbs\thumb_parrot.jpg

Edited by natturefrk
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.