Forum Moderators: coopster

Message Too Old, No Replies

only including certain file types

displaying certain file types when displaying contents of dir?

         

yt404

7:25 pm on Jun 30, 2004 (gmt 0)

10+ Year Member



i have this code which i have taken from php.net and am hopin to have someone tell me how i could modify it to display only one file type (.mid) also this is to stop the script from displaying other directorys as they will automatically load up the index of that directory and won't display a list.

can anyone help out?

here's the code:

<?
$the_array = Array();
$handle = opendir('/home/www/juttuffi/midi/files');
while (false!== ($file = readdir($handle))) {
if ($file!= "." && $file!= "..") {
$the_array[] = $file;
}
}
closedir($handle);
sort ($the_array);
reset ($the_array);
while (list ($key, $val) = each ($the_array)) {
echo "<a href=http://example.co.uk/midi/files/$val target=_blank>$val</a><br>";
}
?>

[edited by: jatar_k at 7:29 pm (utc) on June 30, 2004]
[edit reason] generalized url [/edit]

jatar_k

7:35 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well you could just expand your test here

if ($file!= "." && $file!= "..") {
$the_array[] = $file;
}

so we don't want directories in the array?
!is_dir($file)

and we want to test the file extension to see if it is .mid

$namearr = split('.',$file);
if ($namearr[count($namearr)-1]!= 'mid')

makes sense, so how do we add it

if ($file!= "." && $file!= ".." &&!is_dir($file)) {
$namearr = split('.',$file);
if ($namearr[count($namearr)-1]!= 'mid') $the_array[] = $file;
}

might even work, I sure didn't test it ;)

yt404

10:26 pm on Jun 30, 2004 (gmt 0)

10+ Year Member



nah it had no effect, the link shows that the directory "test" and the .gif file are still visible, heres the code i used.

<?
$the_array = Array();
$handle = opendir('/home/www/juttuffi/midi/files');
while (false!== ($file = readdir($handle))) {
if ($file!= "." && $file!= ".." &&!is_dir($file)) {
$namearr = split('.',$file);
if ($namearr[count($namearr)-1]!= '.mid') $the_array[] = $file;
}
}
closedir($handle);
sort ($the_array);
reset ($the_array);
while (list ($key, $val) = each ($the_array)) {
echo "<a href=http://example.co.uk/midi/files/$val target=_blank>$val</a><br>";
}
?>

i used it as ",mid" and "mid" no effect mate, im not gettin any errors mind so maybe its just the cache, ill check it in an hour but nah no effect. Cheers for the help man

dont spose anyone else or or u maybe know whats up jatar?

[edited by: jatar_k at 10:55 pm (utc) on June 30, 2004]
[edit reason] no personal urls thanks [/edit]

jatar_k

11:05 pm on Jun 30, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



my fault, i made 2 mistakes (i tested it now)

<?
$the_array = Array();
$handle = opendir('/home/www/juttuffi/midi/files');
while (false!== ($file = readdir($handle))) {
if ($file!= "." && $file!= ".." &&!is_dir($file)) {
$namearr = split('\.',$file);
if ($namearr[count($namearr)-1] == 'mid') $the_array[] = $file;
echo "<p>$file<br><pre>";
print_r($namearr);
echo "</pre>";
}
}
closedir($handle);
sort ($the_array);
reset ($the_array);
while (list ($key, $val) = each ($the_array)) {
echo "<a href=http://example.co.uk/midi/files/$val target=_blank>$val</a><br>";
}
?>

forgot to escape the . in the split and had!= 'mid', it would make a lot more sense as == 'mid' ;)

yt404

9:22 am on Jul 1, 2004 (gmt 0)

10+ Year Member



ah nice one mate, it works great. can start making a decent website now.