Jump to content

Why does my loop never end?


JREAM

Recommended Posts

hey i keep crashing my browser with this and it floods it like 100000 times, but i only got like 40 m3ps

 

<script type="text/javascript" src="public/apps/mp3_player/swfobject.js"></script>

<?php

function list_mp3s () {
   $directory = 'public/mp3s/';
   $appdir = 'public/apps/mp3_player/';
   $results = array(); // I dont see why i have this ehre anymore
   $handler = opendir($directory);

$i = 0;   // this is to count unique instances
   while ($file = readdir($handler)) 
   { 
      if ($file != '.' && $file != '..') // look for only filenames
      {
$data .= '<script type="text/javascript">';
$data .= 'var so = new SWFObject("public/apps/mp3_player/playerMini.swf", "mymovie", "75", "30", "7", "#FFFFFF");';
$data .= 'so.addVariable("autoPlay", "no");';
$data .= 'so.addVariable("soundPath", "'.$directory . $file . '");';
$data .= 'so.write("flashPlayer'.$i.'");';
$data .= '</script>';

$data .= '<div id="flashPlayer'.$i.'">';
$data .= 'This text will be replaced by the flash music player.';
$data .= '</div>';

$i++; // increase unique instances

echo $data; // return the stuff.
      }
   }

  
   closedir($handler);
}
// do it
list_mp3s();
?>

Link to comment
https://forums.phpfreaks.com/topic/139365-why-does-my-loop-never-end/
Share on other sites

php.net recommends reading a dir like this:

<?php
// Note that !== did not exist until 4.0.0-RC2

if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
        echo "$file\n";
    }

    /* This is the WRONG way to loop over the directory. */
    while ($file = readdir($handle)) {
        echo "$file\n";
    }

    closedir($handle);
}
?>

 

http://us3.php.net/readdir

 

I believe your just not reading the dir the correct way so your loop never ends...

I would recommend you look at the glob functions. It makes getting the contents of a directory much easier (IMHO).

 

<?php
function list_mp3s () {
   $directory = 'public/mp3s/';
   $appdir = 'public/apps/mp3_player/';
   $results = glob($directory . '*.mp3');
   $i = 0;
   foreach($results as $file) {
        $data = '';
$data .= '<script type="text/javascript">';
$data .= 'var so = new SWFObject("public/apps/mp3_player/playerMini.swf", "mymovie", "75", "30", "7", "#FFFFFF");';
$data .= 'so.addVariable("autoPlay", "no");';
$data .= 'so.addVariable("soundPath", "'. $file . '");';
$data .= 'so.write("flashPlayer'.$i.'");';
$data .= '</script>';

$data .= '<div id="flashPlayer'.$i.'">';
$data .= 'This text will be replaced by the flash music player.';
$data .= '</div>';
$i++; // increase unique instances
echo $data; // return the stuff.
      }
   }
}
// do it
list_mp3s();
?>

 

Ken

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.