Question:
PHP taking images from directories?
Dan
2011-05-11 09:44:44 UTC
I'm building a slideshow and I want to use PHP to generate the slides that way you don't have to physically type in every path because the slideshow is going to contain over 100 images.

So what I'm hoping to do is skip the database phase and just have PHP pull all the files from the directory i tell it too. and then have it run a loop to insert the images into a list. So all I need to figure out is how to tell PHP to grab the .jpg files from the directory?

I'm relatively new to PHP so please try to be specific.

Thanks!
Three answers:
circusmort
2011-05-11 10:09:39 UTC
I do the same - this is a two stage setup I use - the first is a page showing all the different galleries I have (folders inside the galleries folder)




// open directory and make an array of all the folders in the gallery folder

$myDirectory = opendir("images/gallery/");



// get each entry

while (false !== ($file = readdir($myDirectory))) {

if ($file != "." && $file != "..") {

$dirArray[] = $file;

}

}



// close directory

closedir($myDirectory);

sort ($dirArray);



// count elements in array

$indexCount = count($dirArray);





//loop through the array of folders and print them all



for($index=0; $index < $indexCount; $index++) {



$linktext = ($dirArray[$index]);

$pickImage = opendir("images/gallery/".$linktext);



// actually look in the folder and pick an image to display - you could cut this out if you don't want a picture shown



while (false !== ($imageName = readdir($pickImage))) {

if ($imageName != "." && $imageName != "..") {

$picArray[] = $imageName;

}

}



// close directory

closedir($pickImage);

sort ($picArray);



$useThisImage = ($picArray[1]);





echo "
\n";

echo "\n";

$linktext = str_replace("-", " ", $linktext);

echo $linktext;

echo "
\n";



$picArray ="";

}

?>





this is the second page it gets the gallery name from the link in the previous page - if all you want is one gallery and to show all the images in that one alone, only use this code and set the variable $thisGallery in the first line to the relative location of your file.








$thisGallery = "images/gallery/".$_GET['gallery'];

$myImageDirectory = opendir($thisGallery);



while($entryImageName = readdir($myImageDirectory)) {

if ($entryImageName != "." && $entryImageName != ".." ) {

$dirImageArray[] = $entryImageName;

}

}



closedir($myImageDirectory);

sort ($dirImageArray);



$indexImageCount = count($dirImageArray);



for($indexImage=0; $indexImage < $indexImageCount; $indexImage++) {



echo "\n";

}

?>





If you need any further help look for us at tomate design in Mexico
anonymous
2016-10-01 14:38:53 UTC
along with your image resizer you will would desire to jot right down to the itemizing the place the image is residing. If its permissions do no longer enable this, then you rather gets the blunders you have become, wherein case you have another alternatives (in easy terms suitable if this function is new ... if it has worked in the previous it must be a issue with only one corrupt image): a million) rearrange the placement the place all pictures stay to a itemizing the place you do no longer concepts there being a reasonably open permission regime (in line with probability no longer a large concept so a ways as protection is going) 2) use a itemizing with write permissions in easy terms for the writing place, yet bypass away the analyzing place as is (this would have repercussions in different areas of your website with understand to the place you seek for pictures) 3) in line with probability it rather is function to take a seat down at the back of a password secure image itemizing (some information superhighway servers enable password secure directories)
anonymous
2011-05-11 10:26:44 UTC
Hi,

you can try some programming sites like:



http://thefreelancesite.com



They usually have freelance programmers with excellent programming and coding experience.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...