Using PHP to find directory names

I dont know how to go into this without going well overboard with the description so Ill try to be as short hand as I can without leaving out the important bits. I previously looked at a command line solution for this but couldn’t find one. So thinking about a PHP solution instead.

I currently have a tonne of images all saved into various folders in the following layout:

Read More
locationname/hotelname/image.jpg
locationname/businessname/hotelname/image.jpg
locationname/businessname/hotelname/image.jpg
locationname/operatorname/hotelname/image.jpg

Obviously the common denominator here is that the directories always end with the hotel name, luckily the hotel name is the same name as the WordPress Post Title that the images relate to.

Rather than try to rearrange all the $hotelname folders up into the same directory, am I right in thinking I can scan the directories for the lowest dir from where I can then source the images from. A bit like a foreach loop with image sourced like:

<img src="$calculatedurl/image.jpg"/>

Thanks in advance.

Edit: What I want to achieve.

Each Post has a post title and the title is pretty much 100% the same name as the folder in the uploads directory that contains its images. So, for the hotel called “Agrade Apartments”, the images are stored in:

wp-content/uploads/locationame/Agrade Apartments/image.jpg

There are well over 1000 apartments, so rather than try to go through each post and add the images manually, I want to write a conditional statement that will check to see if the image has attachments, if it does then show the images attached, if it doesnt, then get the $posttitle and scan the uploads directories for a folder with the same name, get the contents and foreach $contents as $content add the file to the jQuery slider.

So i roughly end up with:

 $posttitle = the_title();

 $args = array(
 'post_type' => 'attachment',
 'numberposts' => -1,
 'post_status' => null,
 'post_parent' => $post->ID
 );

 $attachments = get_posts( $args );
 $alternativeurl = 
  if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
       echo '<li>';
       echo wp_get_attachment_image_src();
       echo '</li>';
      }
  } else {
  //use a php function to loop through all the folders in the 
  //wp-content/uploads that match the $posttitle variable.
  //when found, loop through each file inside the folder and add 
  //each url to my jQuery slider as a foreach loop.
  }

Hope that helps explain a little further.

Related posts

2 comments

  1. as far as i understand, you like to scan all folder hierarchy and find all images which “belong” to some list of hotels.

    if we suppose what all hotel names are unique and you have access to the file system where the images are, i would recommend you

    1. use readdir() to get contents of the root directory where the images stored
    2. use id_dir() and is_file() to check if the next item is a directory or a regular file
    3. iterate deeper to hierarchy
    4. if a file comply with your requirements (e.g. it’s “*.jpg”) you can get full path of it’s directory using realpath() and split the string using DIRECTORY_SEPARATOR as delimiter
    5. array_pop() will give you the name which is hotel name

    here is some additional info:
    http://php.net/manual/en/ref.dir.php

  2. I have something like that to show all the folders

    $sName = '.';
    $oDir    = opendir($sName);
    $aDirList = array();
    
    while($sDir = readdir($oDir)) {
        if($sDir != '.' && $sDir != '..') {
            if(is_dir($sName.'/'.$sDir)) {
                $aDirList[] = $sDir;
            }
        }
    }
    
    closedir($oDir);
    
    if(!empty($aDirList)) {
        sort($aDirList);
        echo "Folders list : <br>";
        echo "<ul>";
        foreach($aDirList as $sDir) {
            echo "<li><a href="$sName/$sDir ">$sDir</a></li>";
        }
        echo "</ul>";
    }
    

    Maybe they you gonna have some errors but it’s an example of my code …

    Hope I help you ! 🙂

Comments are closed.