natturefrk Posted January 20, 2014 Share Posted January 20, 2014 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 20, 2014 Share Posted January 20, 2014 You can use the RecursiveDirectoryIterator class. Working example http://www.codedevelopr.com/articles/recursively-scan-a-directory-using-php-spl-directoryiterator/ Quote Link to comment Share on other sites More sharing options...
natturefrk Posted January 21, 2014 Author Share Posted January 21, 2014 (edited) 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 January 21, 2014 by natturefrk Quote Link to comment Share on other sites More sharing options...
natturefrk Posted January 21, 2014 Author Share Posted January 21, 2014 (edited) 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.jpgupload\Google\GraffitiCharactersTheCangolier.jpgupload\Google\hammers.pngupload\Google\pictures.pngupload\Google\thumbs\thumb_egret.jpgupload\Google\thumbs\thumb_GraffitiCharactersTheCangolier.jpgupload\Google\thumbs\thumb_hammers.pngupload\Google\thumbs\thumb_pictures.pngupload\Google\thumbs\thumb_turntable.jpgupload\Google\turntable.jpgupload\Test\aboutpage.JPGupload\Test\contactpage.JPGupload\Test\csscert.gifupload\Test\djgraffiti.jpgupload\Test\eggs.jpgupload\Test\filmstrip.jpgupload\Test\fundingpage.JPGupload\Test\homepage.JPGupload\Test\parrot.jpgupload\Test\thumbs\thumb_aboutpage.JPGupload\Test\thumbs\thumb_contactpage.JPGupload\Test\thumbs\thumb_csscert.gifupload\Test\thumbs\thumb_djgraffiti.jpgupload\Test\thumbs\thumb_eggs.jpgupload\Test\thumbs\thumb_filmstrip.jpgupload\Test\thumbs\thumb_fundingpage.JPGupload\Test\thumbs\thumb_graffitiwall2.jpgupload\Test\thumbs\thumb_homepage.JPGupload\Test\thumbs\thumb_parrot.jpg Edited January 21, 2014 by natturefrk Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.