Yii Framework Forum: upload binary data (mp3) - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

upload binary data (mp3) Rate Topic: -----

#1 User is offline   zipzap 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 314
  • Joined: 13-January 12

Posted 19 June 2012 - 10:09 AM

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?
0

#2 User is offline   yiqing95 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 608
  • Joined: 27-December 10
  • Location:china

Posted 19 June 2012 - 11:02 AM

:lol: read this wiki
Storing your images in your table's blob field and displaying that stored images
0

#3 User is offline   zipzap 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 314
  • Joined: 13-January 12

Posted 19 June 2012 - 01:49 PM

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!!
0

#4 User is offline   Roman Solomatin 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 215
  • Joined: 21-October 10
  • Location:Tallinn, Estonia

Posted 19 June 2012 - 08:22 PM

You need to use $_FILES, not $_POST.
0

#5 User is offline   zipzap 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 314
  • Joined: 13-January 12

Posted 20 June 2012 - 10:54 AM

View PostRoman Solomatin, on 19 June 2012 - 08:22 PM, said:

You need to use $_FILES, not $_POST.


No.Doesnt pass on
$mp3->attributes = $_POST['Document'];

0

#6 User is offline   Roman Solomatin 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 215
  • Joined: 21-October 10
  • Location:Tallinn, Estonia

Posted 20 June 2012 - 09:15 PM

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

#7 User is offline   zipzap 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 314
  • Joined: 13-January 12

Posted 28 June 2012 - 05:12 AM

View Postyiqing95, on 19 June 2012 - 11:02 AM, said:


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?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users