mp3 list

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(&...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-30-2004
Alexandre
 
Posts: n/a
Default mp3 list

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;
}

?>

Reply With Quote
  #2 (permalink)  
Old 12-30-2004
Janwillem Borleffs
 
Posts: n/a
Default Re: mp3 list

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);

?>


Reply With Quote
  #3 (permalink)  
Old 12-30-2004
Ray Paseur
 
Posts: n/a
Default Re: mp3 list


"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 ...



Reply With Quote
  #4 (permalink)  
Old 12-30-2004
Janwillem Borleffs
 
Posts: n/a
Default Re: mp3 list

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



Reply With Quote
  #5 (permalink)  
Old 12-30-2004
Daniel Tryba
 
Posts: n/a
Default Re: mp3 list

Alexandre <no@msn.com> wrote:
> $ext = getFileExtension($file);
> if ($ext = "mp3") {


This is always true, check out the difference between '=', '==' and
'===' :)
Reply With Quote
  #6 (permalink)  
Old 12-31-2004
Chung Leong
 
Posts: n/a
Default Re: mp3 list

"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 :
>


You don't have to figure it out. Junk the code and use glob("*.mp3").


Reply With Quote
  #7 (permalink)  
Old 01-05-2005
Ray Paseur
 
Posts: n/a
Default Re: mp3 list


"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


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:44 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0