upload binary data (mp3)

How to insert a mp3 file to database

Models:

Document(where i store the binary data):





<?php


/**

 * This is the model class for table "document".

 *

 * The followings are the available columns in table 'document':

 * @property integer $idbinarios

 * @property string $filename

 * @property string $content

 *

 * The followings are the available model relations:

 * @property Ritmo[] $ritmos

 * @property Ritmo[] $ritmos1

 */

class Document extends CActiveRecord

{

        /**

         * Returns the static model of the specified AR class.

         * @param string $className active record class name.

         * @return Document the static model class

         */

        public static function model($className=__CLASS__)

        {

                return parent::model($className);

        }


        /**

         * @return string the associated database table name

         */

        public function tableName()

        {

                return 'document';

        }


        /**

         * @return array validation rules for model attributes.

         */

        public function rules()

        {

                // NOTE: you should only define rules for those attributes that

                // will receive user inputs.

                return array(

                        array('idbinarios, filename, content', 'required'),

                        array('idbinarios', 'numerical', 'integerOnly'=>true),

                        array('filename', 'length', 'max'=>345),

                        array('content', 'file'),

                        // The following rule is used by search().

                        // Please remove those attributes that should not be searched.

                        array('idbinarios, filename, content', 'safe', 'on'=>'search'),

                );

        }


        /**

         * @return array customized attribute labels (name=>label)

         */

        public function attributeLabels()

        {

                return array(

                        'idbinarios' => 'Idbinarios',

                        'filename' => 'Filename',

                        'content' => 'Content',

                );

        }


        /**

         * Retrieves a list of models based on the current search/filter conditions.

         * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

         */

        public function search()

        {

                // Warning: Please modify the following code to remove attributes that

                // should not be searched.


                $criteria=new CDbCriteria;


                $criteria->compare('idbinarios',$this->idbinarios);

                $criteria->compare('filename',$this->filename,true);

                $criteria->compare('content',$this->content,true);


                return new CActiveDataProvider($this, array(

                        'criteria'=>$criteria,

                ));

        }

}






and

Ritmo where i have a foreign key to the documentid:

‘mp3’ => array(self::BELONGS_TO, ‘Document’, ‘mp3’),

the idea is to have an activefilefield and uploading to database

what is best?Is to store on database or store files on disk locally?

:lol: read this wiki

Storing your images in your table’s blob field and displaying that stored images

I red the article and i have this on ritmo _form:


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'ritmo-form',

	'enableAjaxValidation'=>false,

        'htmlOptions'=>array('enctype'=>'multipart/form-data'),

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($ritmo); ?>


	<div class="row">

		<?php echo $form->labelEx($ritmo,'artist'); ?>

		<?php echo $form->textField($ritmo,'artist',array('size'=>45,'maxlength'=>45)); ?>

		<?php echo $form->error($ritmo,'artist'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($ritmo,'title'); ?>

		<?php echo $form->textField($ritmo,'title',array('size'=>45,'maxlength'=>45)); ?>

		<?php echo $form->error($ritmo,'title'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($ritmo,'price'); ?>

		<?php echo $form->textField($ritmo,'price',array('size'=>10,'maxlength'=>10)); ?>

		<?php echo $form->error($ritmo,'price'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($ritmo,'mp3'); ?>

                <?php echo $form->fileField($mp3,'content',array('size'=>60)); ?>

		<?php echo $form->error($ritmo,'mp3'); ?>

	</div>

     <div class="row">

		<?php echo $form->labelEx($ritmo,'youtubeLnk'); ?>

		<?php echo $form->textField($ritmo,'youtubeLnk',array('size'=>60));  ?>

		<?php echo $form->error($ritmo,'youtubeLnk'); ?>

	</div>

	

	<div class="row">

		<?php echo $form->labelEx($ritmo,'categoria'); ?>

		<?php echo $form->dropdownlist($ritmo,'categoria',$ritmo->Categorias) ?>

		<?php echo $form->error($ritmo,'categoria'); ?>

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton($ritmo->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php $this->endWidget(); ?>


</div>

Now on my update controller action:


 public function actionUpdate($id) {

        $ritmo = $this->loadModel($id);

	$mp3=new Document;

        $imagem=new Document;

        

        if (isset($_POST['Ritmo'])) {

            $ritmo->attributes = $_POST['Ritmo'];

           

        if (isset($_POST['Document'])) 

            $mp3->attributes = $_POST['Document']; 

            

         

        }


        $this->render('update', array(

            'ritmo' => $ritmo,

            'mp3'=>$mp3

        ));

    }



but $_POST[‘Document’] isnt set!!

You need to use $_FILES, not $_POST.

No.Doesnt pass on


$mp3->attributes = $_POST['Document'];

Oh, sorry, I misred your post. I’ll try to give it another look.

i red that wiki and i modify my code like this:


public function actionCreate() {

        $ritmo = new Ritmo;

        $mp3=new Document;


        // Uncomment the following line if AJAX validation is needed

        // $this->performAjaxValidation($model);

        if(isset($_POST['Document']))

        {

            $mp3->attributes=$_POST['Document'];

 

            if(!empty($_FILES['Document']['tmp_name']['content']))

            {

                $file = CUploadedFile::getInstance($mp3,'content');

                $mp3->filename = $file->name;

                $mp3->type = $file->type;

                $fp = fopen($file->tempName, 'r');

                $content = fread($fp, filesize($file->tempName));

                fclose($fp);

                $mp3->content = $content;

                

                $mp3->save();

            }

        }

       


        $this->render('create', array(

            'ritmo' => $ritmo,

            'mp3'=>$mp3

        ));

but doesnt save anything on database and no error,if i echo de filename,type and content the data is there.Is any log i can check?