serving files outside webroot and http headers help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
homerjsimpson
Forum Newbie
Posts: 3
Joined: Sat Apr 21, 2007 9:12 am

serving files outside webroot and http headers help

Post by homerjsimpson »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I want to keep mp3 files away from the public but allow them  to download the mp3 files if they are members to my site.  They can also listen to the mp3 files through an embedded media player on the site and if they wanted to, they can download via a php script.

in the embedded script I point the src to the mp3 file as /home/me/mp3/example.mp3 and it works.  

My problem is how to make the file accessible when the user trys to file.  I am using the follwing code for testing purposes:

Code: Select all

<?
$file = @file_get_contents("/home/me/example.mp3");
    if($file != false){
        header('Content-Type: application/octet-stream');
        echo $file; }
?>
thats the only code I have in the php file and when i point the browser to the php file, it downloads the php file. when i open the php file i see a bunch of jibberish.

any help would be much appreciated. thanks


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You almost had it ;)

viewtopic.php?p=53557#53557
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

THis might help you
viewtopic.php?t=64478
homerjsimpson
Forum Newbie
Posts: 3
Joined: Sat Apr 21, 2007 9:12 am

Post by homerjsimpson »

ok I figured out how to force download and all is working fine for files located outside the webroot, but i spoke to soon in regards to the embedded player being able to play the mp3 file located outside of the webroot.

here is the code but it won't play:

Code: Select all

<OBJECT ID="MediaPlayer" WIDTH=320 HEIGHT=240
classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"
codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Vers standby="Loading Microsoft Windows Media Player components..."
type="application/x-oleobject">
    <PARAM NAME="FileName" VALUE="some.mp3">
    <PARAM NAME="ShowControls" VALUE="1">
    <PARAM NAME="ShowDisplay" VALUE="1">
    <PARAM NAME="ShowStatusBar" VALUE="1">
    <PARAM NAME="AutoSize" VALUE="1">
    <Embed type="application/x-mplayer2"

pluginspage="http://www.microsoft.com/windows/windowsmedia/download/AllDownloads.asp filename="some.mp3"
        src="/home/me/some.mp3"
        Name=MediaPlayer
        ShowControls=1
        ShowDisplay=1
        ShowStatusBar=1
        width=320
        height=240>
    </embed>
</OBJECT>
I even specified the sorce to be ../some.mp3 (the mp3 file is at the top level directory of my server) still no go. what can I do? thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you need to serve the content through a php script using readfile()

Code: Select all

src="media_loader.php?id=some.mp3"
Post Reply