Playing music

Good day to everyone,

I would like to ask how you can play a music that is in protected folder. I'm building a website that uploads .mp3 files to the server as told by this webpage The problem is the website must be able to play this .mp3 files but I put them on a protected folder. I have read somewhere, although I can not remember the webpage, that I must write a new method on the controller to access the files. I have a clue on how I can access the file from the controller but I have no idea how I'm gonna play the music once I get the file? Or if you have know better method for doing this I would be glad if you can share it.

Any help with be appreciated. Thank you in advance.

Welcome to the forum!

Why do you store mp3 files in the protected directory? It is called "protected" because it should not be accessable from the web. Put these files into webroot/mp3 and use any mp3 player you like (you can find many of them, e.g.

Still, you can use protected directory to store these files, but you’ll need to create a controller action to render them. I’m not sure it is the easiest way until you want to check user permissions and etc.

Thank you for the response. I’m putting it on the protected folder cause I don’t want everyone to be able to access the mp3 files. The mp3 files must be secure and only certain users must be able to access these files. I do think the best way is to create a controller action to get the file but afterwards I have no idea how I can play the mp3 files because all the players need a direct link to the file that is accessible on the server.

I think I should have used a FTP client though I don’t really know how to. I am willing to learn this but if it is possible to solve this problem using the basic file manipulation provided by php and built in support by yii then It would be better.

Links can be different and if a link looks like “http://site/music/file.mp3” it doesn’t mean the file is stored under the “music” directory :)

First, create an action (e.g. mp3) which recieves a track id (if you store some info about files in a database) or it’s filename. Inside an action try to use sendFile() or xSendFile(). I’m not sure this will work immediately, and maybe you’ll need to send some special headers, but it is close to the right way ;)