Değişken (belki ajax?) Kaybolursa

0 Cevap

Ben oldukça basit bir komut dosyası var ama bir şey yanlış gidiyor ve ben onu anlamaya görünüyor olamaz. Benim komut dosyası, bir klasördeki dosyaları toplar rasgele bir dosya seçer. Rasgele seçilen dosya bu dizide değilse kontrol etmek için başka bir dizi karşı kontrol edilir. Bu kontrol dizisi aslında #-karakter üzerinde patlayan bir diziye koymak textstream olduğunu.

Ses çalınabilir bir metin dosyası henüz değilse komut, bir koleksiyon rastgele bir ses çalmak için kullanılır. Ses textfile yanıkta ise başka bir rasgele bir dosya seçilir (ve böylece .. özyineleme) gerekmektedir. Garip bir şey özyinelemeli fonksiyon tüm iyi gidiyor ama ben bu işlevin sonucunu görüntülemek zaman emin fonksiyon oynadı (ve metin dosyası içine saklanan) için bir dosya dönen iken, bazen sadece boş gösteriyor olmasıdır.

PHP komut dosyası XMLHTTPObject yoluyla denir. Bu değişken kaybolursa bir nedeni olabilir mi? Ya benim komut dosyası başka bir şey eksik?

The script:

<?php
    header("Cache-Control: no-cache, must-revalidate");
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); 

    $soundList      = get_folder_entries("playlist", array("wma", "wav", "mp3"));
    $playedFile     = "played.txt";
    $playedContents = file_get_contents($playedFile);
    $playedSounds   = explode("#", $playedContents);

    // if all files are played just select a new file and reset played.txt  
    if(count($playedSounds) == count($soundList))
    {
        $fileHandle = fopen($playedFile, "w");  
        $file       = $soundList[mt_rand(0, count($soundList) - 1)];

        fwrite($fileHandle, $file."#");
        fclose($fileHandle);

        echo "<input type=\"hidden\" id=\"SoundToPlay\" value=\"".$file."\"/>";
    }
    // pick a random file which has not yet been played
    else
    {
        $file       = pick_random_file($soundList, $playedSounds);
        $fileHandle = fopen($playedFile, "w");

        fwrite($fileHandle, $playedContents.$file."#");
        fclose($fileHandle);

        echo 'INPUT = '.$file;

        echo "<input type=\"hidden\" id=\"SoundToPlay\" value=\"".$file."\"/>";
    }

    // some debug/output info
    echo "<table width=\"100%\">";
    echo "<tr>";
    echo "<td class=\"header\"><h2>[Array] Playlist</h2></td>";
    echo "<td class=\"header\"><h2>[Array] Played</h2></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td><pre>";
    print_r($soundList);
    echo "</pre></td>";
    echo "<td><pre>";
    print_r($playedSounds);
    echo "</td>";
    echo "</tr>";
    echo "</table>";    

    // collect the files from a folder with a specific extension
    function get_folder_entries($dir, array $extensions)
    {
        $fileList = array();

        if($fileHandle  = opendir($dir))
        {
            while(false !== ($file = readdir($fileHandle)))
            {
                if($file != "." && $file != ".." && in_array(end(explode(".", $file)), $extensions))
                {
                    array_push($fileList, $file);
                }
            }
        }

        return $fileList;
    }

    // recursive function to pick a random sound
    function pick_random_file(array $soundList, array $playedSounds)
    {
        $sound = $soundList[mt_rand(0, count($soundList) - 1)];

        if(!in_array($sound, $playedSounds))
        {
            return $sound;  
        }
        else
        {
            pick_random_file($soundList, $playedSounds);    
        }
    }

0 Cevap