Computing.Net > Forums > Web Development > PHP Sub directories

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP Sub directories

Reply to Message Icon

Name: RTAdams89
Date: March 24, 2008 at 11:24:42 Pacific
OS: na
CPU/Ram: na
Product: na
Comment:

As part of a image rotation script I use PHP to find all picture files in some directories and include them in a JavaScript array. Here is my current code:

<?php
function returnimages($dirname, $start) {
$pattern="(\.jpg$)|(\.jpeg$)"; //valid image extensions
$curimage=$start;
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if(eregi($pattern, $file)){ //if this file is a valid image
//Output it as a JavaScript array element
echo 'galleryarray['.$curimage.']="'.$dirname.''.$file .'";';
$curimage++;
}
}
closedir($handle);
}
return($curimage);
}

$start = 0;
echo 'var galleryarray=new Array();'; //Define array in JavaScript
$start = returnimages("gallery/All Teams/", $start);
$start = returnimages("gallery/Varsity/", $start);
$start = returnimages("gallery/Junior Varsity/", $start);
$start = returnimages("gallery/Freshman/", $start);
$start = returnimages("rosters/", $start);
?>

As you can see, I have to define sub-directories manually at the end of the script. Is there an easy way to have the PHP script automatically include all sub-directories inside of "/gallery/"?

-Ryan Adams
http://RyanTAdams.com



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: March 24, 2008 at 17:02:19 Pacific
Reply:

<?php

function returnimages($dirname, $curimage=0) {
$pattern="(\.jpg$)|(\.jpeg$)"; //valid image extensions
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){

if (is_dir("$dirname/$file") && $file!='.' && $file!='..') {
$curimage = returnimages("$dirname/$file", $curimage);
} else {
if(eregi($pattern, $file)){ //if this file is a valid image
//Output it as a JavaScript array element
echo "galleryarray[$curimage]='";
echo "'".str_replace('//','\\',$dirname)."\\$file';\n";
$curimage++;
}
}
}
closedir($handle);
}
return($curimage);
}

echo "var galleryarray=new Array();\n"; //Define array in JavaScript
$start = returnimages("gallery/");

?>

Michael J


0

Response Number 2
Name: RTAdams89
Date: March 24, 2008 at 19:33:52 Pacific
Reply:

I got that working by changing a few slashes and quotes. Here is the code that I have now:

<?php
function returnimages($dirname, $curimage=0) {
$pattern="(\.jpg$)|(\.jpeg$)"; //valid image extensions
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
if (is_dir("$dirname/$file") && $file!='.' && $file!='..') {
$curimage = returnimages("$dirname/$file", $curimage);
} else {
if(eregi($pattern, $file)){ //if this file is a valid image
//Output it as a JavaScript array element
echo "galleryarray[$curimage]=";
echo "\"".str_replace('//','\\',$dirname)."/$file\";";
$curimage++;
}
}
}
closedir($handle);
}
return($curimage);
}
echo "var galleryarray=new Array();"; //Define array in JavaScript
$start = returnimages("gallery");
?>

Is there a way to define two directories?


0

Response Number 3
Name: Michael J (by mjdamato)
Date: March 24, 2008 at 20:18:05 Pacific
Reply:

This should work:

$start = returnimages("directory1");
$start = returnimages("directory2", $start);

Michael J


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: PHP Sub directories

readdir PHP www.computing.net/answers/webdevel/readdir-php/1948.html

Newbie PHP question www.computing.net/answers/webdevel/newbie-php-question/1563.html

PHP Sort directory contents by date www.computing.net/answers/webdevel/php-sort-directory-contents-by-date/3483.html