Music Stream echo wrong links to mp3 files

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Wed Jan 08, 2014 9:29 pm

Music Stream echo wrong links to mp3 files

Postby Fijiannn » Wed Jan 08, 2014 9:31 pm

Below I have this PHP script and a folder called "songs" on my host. What my script does is it access the mp3 music in my songs folder and shuffles them. Then creates an array of links to each song that outputs through $body. The problem I seem to be having is when I test this script on my iPhone app, it outputs all the links in the songs folder in my console and the music begins to stream. The first link I would think would be the first song that is streaming but it's not. What is wrong with my code? Below is my my PHP script.

Code: Select all


    function myFunction($url) {
        $a = join('/', array_map('urlencode', explode('/', $url)));
        return str_replace('+', '%20', $a);

    $SongsFolder = "songs";

    header("Content-Type: audio/x-mpegurl");

    $body = '';

    $Songs = glob($SongsFolder."/*.mp3");
    $first = 0;

    foreach($Songs as $Song)
        $SongPath = pathinfo($Song);
        if ($first != 0) {
            $body .= "\n";
        $first = 1;
        $body .= "http://".$_SERVER['HTTP_HOST'].myFunction(dirname($_SERVER['PHP_SELF']))."/".myFunction($SongsFolder)."/".myFunction($SongPath['basename']);
    header('Content-Length: ' . strlen($body));
    echo $body;

Here is what my console outputs.

Code: Select all

2014-01-08 21:03:50.411 Virsa[2865:54d2f]

Return to “PHP coding => General”

Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests