This is a discussion on mp3 list within the PHP Language forums, part of the PHP Programming Forums category; Hi I can't figure why I can't list only mp3 files with this code : <?php $dir = opendir(&...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi I can't figure why I can't list only mp3 files with this code :
<?php $dir = opendir("."); while($file = readdir($dir)) { $ext = getFileExtension($file); if ($ext = "mp3") { echo "<a href=" .$file.">".$file; echo "<p>"; } } closedir($dir); function getFileExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } ?> |
|
|||
|
Alexandre wrote:
> Hi I can't figure why I can't list only mp3 files with this code : > Try this instead: <?php $dir = opendir("."); while($file = readdir($dir)) { if (preg_match("/\.mp3$/i", $file)) { print "<p><a href=\"$file\">$file</a></p>\n"; } } closedir($dir); ?> |
|
|||
|
"Alexandre" <no@msn.com> wrote in message news:41d4182a$0$32611$5402220f@news.sunrise.ch... > Hi I can't figure why I can't list only mp3 files with this code : > > <?php > $dir = opendir("."); > while($file = readdir($dir)) { > $ext = getFileExtension($file); > if ($ext = "mp3") { > echo "<a href=" .$file.">".$file; > echo "<p>"; > } > } > closedir($dir); > > function getFileExtension($str) { > $i = strrpos($str,"."); > if (!$i) { return ""; } > $l = strlen($str) - $i; > $ext = substr($str,$i+1,$l); > return $ext; > } > > ?> > Alexandre, you might try something like this instead of the function: while($file = readdir($dir)) { $x = strrev($file); if (eregi('^3PM\.', $x) {echo "<a href ... etc ... |
|
|||
|
Ray Paseur wrote:
> Alexandre, you might try something like this instead of the function: > > while($file = readdir($dir)) { > $x = strrev($file); > if (eregi('^3PM\.', $x) {echo "<a href ... etc ... Just being curious, but why the strrev when you can also do the following: if (eregi('\.MP3$', $file) {echo "<a href ... etc ... JW |
|
|||
|
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message news:41d41c50$0$58061$1b2cd167@news.euronet.nl... > Ray Paseur wrote: >> Alexandre, you might try something like this instead of the function: >> >> while($file = readdir($dir)) { >> $x = strrev($file); >> if (eregi('^3PM\.', $x) {echo "<a href ... etc ... > > Just being curious, but why the strrev when you can also do the following: > > if (eregi('\.MP3$', $file) {echo "<a href ... etc ... > > I can't remember! I snipped that out of a piece of code that needed to be reversed for some other things I was doing. Happy New Year, ~Ray |