views:

135

answers:

3

ok so i have a folder that was created after this answer was used

so now i have a folder with with many folders in it each one having images and a .txt file

the images are almost all .jpg

so now i want to display a image from each folder, using php i want to display the first image from each folder, and if there is no images i want to display a default image

so by just have the location of the folder i want to display the image

any ideas? it can be through php or javascript, no other languages please it will untimely be used in a wordpress theme with the folder location being a custom field parameter

+2  A: 

Try using PHP glob - it returns an array of filenames in any folder you give it. You can filter by *.jpg, and just use the first item in the array.

For example, I use:

$imageArray = glob($folder . "/image*.jpg");

which makes an array of all filenames called image[something].jpg in the folder $folder.

Skilldrick
thanks for the glob
vache
A: 

wow, I wonder if you can have your entire program written for you by just asking questions!

try to do it yourself, post your best try, and ask specific questions when you run into a problem.

KM
well as you see i did not receive a full program, just a name of a function. so hey thanks for your answer :)
vache
A: 
<?php

$record_ratio = 0;
foreach (glob("*.jpg") as $filename) {
    $info = getimagesize($filename);
    $ratio = $info[0] / $info[1];
    if (abs(1 - $ratio) < abs(1 - $record_ratio)) {
      $record_ratio = $ratio;
      $record_filename = $filename;
    }
    if (record_ratio == 1) break;
}
if ($record_ratio > 0) {
  echo '<img src="'.$record_filename.'" height=150px><br>';
}

?>

<?php
foreach (glob("*.txt") as $filename) {
     echo nl2br(file_get_contents($filename));
        echo "<br></br>";
}
?>
vache