download file from database

I am trying to download a mp3 file that i have in database to play with strobemediaplayer.strobemediaplayer need to have the file in assests folder so i have this to write the file:


$mp3 = Document::model()->findByPk($_POST['mp3Lnk']);


            

             $fp = fopen(Yii::app()->request->baseUrl . '/assets/mp3/temp.mp3', 'wb');

             fwrite($fp, $mp3->content);

but i have this error:

failed to open stream: No such file or directory

Now i can download the file right like this:


$caminho = Yii::getPathOfAlias('webroot') . '/assets/mp3/temp.mp3';

            $fp = fopen($caminho, 'wb+');

            fwrite($fp, $mp3->content);

But for some reason i dont know why the mp3 file in database just has 64KB

That’s what you get for saving mp3’s in database.

Change your approach, save them to the disk and keep only the file location in the database.

Usually, as Twisted1919 points out, you save the original filename and the location of the file in the database, not the file itself.

To avoid name collisions you normally save the file under a different name, most often a hashed name, so you need to original filename if you want to be user friendly.

No.I got that because my content field was a blob ,now with the change to medium blob works preety fine.

change your approach, for your own good do as i say (otherwise you’ll regret when you’ll have a 1Gb database of mp3 and you’ll want to move it around).