I'm thinking of hosting a name that tune type game.

I don't know anything about PHP or even if it's the languge to use, but I'm hoping someone
might put me straight on that matter too.

Here is an outline of my needs/scenario.
I post a link to a song.mp3 I have uploaded to my host, the user has to guess the song and artist.
The problem is, as I'm sure most here will have immediately envisioned is the link to the song title
will be visible to anyone with some knowledge of how this sort of thing works.
I could change the title and meta data of the file, but that seems overkill.
I want to make a link to say Guess.mp3, but when that link is clicked it serves up an entirely different
Song.mp3 which is determined by a php script which cannot be seen or found without a fair bit of jiggery pokery.

I hope you follow what I mean and can advise if such a thing is even possible.
My hopes are that the link can be posted not on my own website, but for instance here at daniweb.
Any advice or suggestions of other methods are very welcome and much appreciated.

Recommended Answers

All 24 Replies

You can point the audio to a PHP file (guess.php). It can return anything you want. If you set the right headers it can return an audio file too.

<audio controls autoplay>
    <source src="guess.php" type="audio/mpeg" />
    Your browser does not support the audio element.
</audio>

Then your guess.php has to read and return an mp3 file with the right headers.

Thanks for your time.

I have this in my guess.php file

//http://www.longtailvideo.com/support/forums/jw-player/setup-issues-and-embedding/3819/serving-a-mp3-file-with-php/

<?php
$fc = file_get_contents("NewMp3.mp3");
$size = filesize("NewMp3.mp3");
header('Content-type: audio/mpeg');
header('Content-length: ' . $size);
header('Content-Disposition: filename=NewMp3.mp3');
header('X-Pad: avoid browser bug');
Header('Cache-Control: no-cache');
echo $fc;
?>

If I navigate to the page http://www.domian.com/guess.php then a media player shows and the song plays. But I need the same to happen withing media tags on another website/forum.

I can't try right now, but any media method you use that accepts an url, should also accept your php script.

Hi Suzie99,

Can you add the content transfer encoding field and tell it's binary.
Content-transfer-encoding: binary
header('Content-transfer-encoding: binary')
Let me know what happened.

Hi gon1387, Thank you for reply, unfortunately there is no change.
Does it matter the order of header values?

No actually, unless you put the contents on top. LOL :D
Anyway joking asside. I've seen this one, I think it's the content type.
Q&A

Anyway, I;ve checked your script, you got a capitalized header func:

me=NewMp3.mp3');
header('X-Pad: avoid browser bug');
Header('Cache-Control: no-cache'); \\make the 'H' lower-case

I noticed that earlier and changed it, no luck :(
(edit)It does not seem to matter, I just tried them all capitalised, without change.

I was checking on the different encoding for MP3 and content-types. Any luck with the mime types in their solution?

Hi, I tried without success.
I also read in that article that nothing should precede header, so had to remove filesize
Code now looks like this, but still no good.

<?php
header('Content-type: audio/mpeg');
header('Content-Disposition: filename=NewMp3.mp3');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
header('Content-transfer-encoding: binary');
$fc = file_get_contents("NewMp3.mp3");
echo $fc;
?>

Actually, this method might not be very good, because if I navigate to the php page in browser and view source, the meta data is the first thing to see at the top with artist and title.

:(

What it's trying to say with "No output before the header" is there should be data to be output in the stream befor ethe header. When the HTML tag was included above, and before the '<?php' it already send a set of character to the output stream, which is the "<html>".

So the reason behind this is when you send the out put using the HTTP protocol(not in the context of PHP) it would look like this:

<html>
Location: http://www.example.com/
... other http fields ...

Which means, if read by the reciever is a malformed HTTP. Another way of expressing the sample in the PHP Manual is this:

<?php
echo "<html>";
/* This will give an error. Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
?>

So what you did earlier is right. You have to put the length especially if it's a get request, so the reciever, which is the browser would know when to stop listening for incoming data.

Anyhow, here's the sample code from SO, using your code:

<?php
$fc = file_get_contents("NewMp3.mp3");
$size = filesize("NewMp3.mp3");
header("Content-Transfer-Encoding: binary");
header('Content-type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3');
header('Content-length: ' . $size);
header('Content-Disposition: filename="NewMp3.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');

echo $fc;

Let's hope this one works :D

Anyway, it's working on my end. How 'bout yours?

Nope, unless I'm doing something wrong.
I still get the link to it, and also, it does not try to play, and rather wants to download, and my browser dialog opens for me to choose a location.

I have just the php code, with no html at all on the page.

One question though, if you access in your browser.
yourdomain.com/mp3.php

does it download the right mp3 file you specified in your php file?

Yes, and plays fine with all meta data in tact.
I really do appreciate the time you have put into your generous assistance.
I'm beginning to think I might as well just remove meta data, and rename files, just seems too much.

The html works fine, shows a little player, and plays just great.

<!DOCTYPE html>
<html>
<body>

<audio controls>
  <source src="NewMp3.mp3" type="audio/mpeg">
  <embed height="50" width="100" src="NewMp3.mp3">
</audio>

</body>
</html>

Sorry, I have to remove it my response. I think you already got it sorted out..

I have not got it sorted, maybe I gave the wrong impression (it's how my first was born).
This just puts a player in a page on my site and clearly shows the song name, which is the whole point of my problem.

ok, here we go again. I am not going to provide any link because, for some reason the mp3 I would like to be the demo for this response is misbehaving badly :).

I only tested this on IE, because that is the only one I have at the moment.

Here we go, we can provide link from any page using this syntax

    <a href="mp3streamer.php?file=pinkfloyd">Pink Floyd </a>

Ok, just to remind you that the file name is up to you. YOu can rename your mp3 like mysterysong101.mp3, it does not matter.

Here is the script..

    <?php
    ## Warning! not tested in firefox ONLY in IE. This script triggers the windows media player.

    if(isset($_GET['file']) && (!empty($_GET['file']))){
     $thisMp3 = mp3Streamer($_GET['file'],'.mp3');
    }
    else{

    //nothing 

    }

    function mp3Streamer($title,$ext){

    ## define secret directory
    $loc = 'uploads/';

    ## based on the known vars we have the actual filename, location, and extension
    $actual_file = $loc.$title.$ext;
    ## mime types possible
    $mType = "  audio/mpeg3, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg"; 
    ## we double check and make file exist
    if(file_exists($actual_file)){

    ## do as what we already know
        header('Content-type: {$mType}');
        header('Content-length: ' . filesize($actual_file));
        header('Content-Disposition: filename="'. $actual_file .'"');
        header('X-Pad: avoid browser bug');
        header('Cache-Control: no-cache');
        $mfile = readfile($actual_file);

        ## ! source src was left blank intentionally ##
        ###############################################

        $thisAudio = '<audio controls height="100" width="100">
      <source src="" type="audio/mpeg">
      <embed height="50" width="100" src="'.$mfile.'">
       </audio> ';
    die(); 
    }

    else{

    $thisAudio = 'not available';

    }

    return $thisAudio;

    }

    ?>

Good luck to you.. :)

Hi veedeoo, thank you for your time and effort.
Your script works fine as it is supposed to, but unfortunately is the same as
the codes above, in that if I view the page source, the title and artist are clearly visible, and it does not work in media tags either in chrome, IE or firefox.

When I started this thread, It was my hope that I could have a link "www.domain.com/fakemp3.mp3" but when navigated to of put in tags, a different song would be served.

Thank you all once again for trying, but I believe it not possible at this time and try to think of another method of acheiving my goal.

Cheers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.