By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,965 Members | 1,702 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,965 IT Pros & Developers. It's quick & easy.

first file from every subfolder!

P: n/a
How do I get a list of the first file of every subfolder?
This is what i have so far
<?php
function getfirstImage($dirname)
{
global $imageName;
$ext = array("jpg", "png", "jpeg", "gif", "JPG", "PNG", "GIF",
"JPEG");
if($handle = opendir($dirname))
{
while(false!== ($file = readdir($handle)))
{
if(strstr($file, "." . $ext[$i])!= '.' && strstr($file, "." .
$ext[$i])!= '..')
{
break;
}
}
$imageName = $file;
closedir($handle);
}
return($imageName);
}

echo getfirstImage('../galleri/galleri/diverse/');
?>

$getfirstImage(../galleri/galleri/)
Aug 18 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Aug 18, 12:11*pm, Effix <Morteneist...@gmail.comwrote:
How do I get a list of the first file of every subfolder?
To do this for every subfolder, you would make a readdir loop which
determines whether the file is a directory (is_dir) and call
getfirstImage() for each directory.

function getfirstImage($dirname)
{
$imageName = false;
$ext = array("jpg", "png", "jpeg", "gif", "JPG", "PNG", "GIF",
"JPEG");
if($handle = opendir($dirname))
{
while(false !== ($file = readdir($handle)))
{
$lastdot = strrpos($file, '.');
$extension = substr($file, $lastdot + 1);
if ($file[0] != '.' && in_array($extension,
$ext))
{
break;
}
}

$imageName = $file;
closedir($handle);

}
return($imageName);
}
Aug 18 '08 #2

P: n/a
Effix wrote:
How do I get a list of the first file of every subfolder?
This is what i have so far
<?php
function getfirstImage($dirname)
{
global $imageName;
$ext = array("jpg", "png", "jpeg", "gif", "JPG", "PNG", "GIF",
"JPEG");
if($handle = opendir($dirname))
{
while(false!== ($file = readdir($handle)))
{
if(strstr($file, "." . $ext[$i])!= '.' && strstr($file, "." .
$ext[$i])!= '..')
{
break;
}
}
$imageName = $file;
closedir($handle);
}
return($imageName);
}

echo getfirstImage('../galleri/galleri/diverse/');
?>

$getfirstImage(../galleri/galleri/)
Where are you setting/incrementing $i?
Why aren't you just doing
if ($file != '.' && $file != '..')
if those are the two you want to bypass. If you THEN want to check that
it is one of your extension types, then do a foreach on $ext.
Aug 18 '08 #3

P: n/a
sheldonlg wrote:
Effix wrote:
>How do I get a list of the first file of every subfolder?
This is what i have so far
<?php
function getfirstImage($dirname)
{
global $imageName;
$ext = array("jpg", "png", "jpeg", "gif", "JPG", "PNG", "GIF",
"JPEG");
if($handle = opendir($dirname))
{
while(false!== ($file = readdir($handle)))
{
if(strstr($file, "." . $ext[$i])!= '.' && strstr($file, "." .
$ext[$i])!= '..')
{
break;
}
}
$imageName = $file;
closedir($handle);
}
return($imageName);
}

echo getfirstImage('../galleri/galleri/diverse/');
?>

$getfirstImage(../galleri/galleri/)

Where are you setting/incrementing $i?
Why aren't you just doing
if ($file != '.' && $file != '..')
if those are the two you want to bypass. If you THEN want to check that
it is one of your extension types, then do a foreach on $ext.

....also, what happens if a file is named foo.JpG ??
Aug 18 '08 #4

P: n/a
On 18 Aug, 11:11, Effix <Morteneist...@gmail.comwrote:
How do I get a list of the first file of every subfolder?
How are you defining "first"?
Aug 18 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.