Yii Framework Forum: issue with uploading to database - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

issue with uploading to database Rate Topic: -----

#1 User is offline   caly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 26-July 09

Posted 27 July 2009 - 08:56 PM

I'm using this code to upload files to the database, while all the attributes are correct, the actual file doesn't seem to upload correctly.

public function actionCreate()
{
$model=new Report;
if(isset($_POST['Report']))
{
$file = CUploadedFile::getInstance($model,'data');
$model->name=$file->name;
$model->type=$file->type;
$model->size=$file->size;
$model->data=$file;

if($model->save()) {
$this->redirect(array('show','id'=>$model->id));
}
}
$this->render('create',array('model'=>$model));
}

Example, instead of seeing this in the database table:

app.txt text/plain 1496 [BLOB - 1.5 KiB]

I see this:

app.txt text/plain 1496 [BLOB - 7 B]


Anyone know why?

Thanks!
0

#2 User is offline   sidewinder 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 218
  • Joined: 08-July 09
  • Location:Poland

Posted 28 July 2009 - 12:25 AM

Hello caly,

your problem is in this line:

$model->data=$file;


$file is an instance of CUploadedFile and contains some information about uploaded file but doesn't return the uploaded file contents. You need different approach to save file to DB.

You should use a php function like file_get_contents() to read file contents. http://pl2.php.net/file_get_contents
The best option IMO is to go like that:

   $file = CUploadedFile::getInstance($model,'data');
    if(!$file->getHasError()){
         $model->name=$file->name;
         $model->type=$file->type; 
         $model->size=$file->size;
         $model->data=file_get_contents($file->getTempName());
    }
     


I hope that helps :) .
---------------------------------------------------------------------
"Never memorize what you can look up in books."
Albert Einstein
0

#3 User is offline   knut 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 45
  • Joined: 10-October 08
  • Location:Notodden, NORWAY

Posted 28 July 2009 - 01:21 AM

You should check for file upload errors with CUploadedFile::getHasError() and CUploadedFile::getError() before saving your record to avoid broken uploads.
0

#4 User is offline   caly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 26-July 09

Posted 28 July 2009 - 08:14 PM

Thank you both! With the added error check and file_get_contents call, the files upload perfectly now.
0

#5 User is offline   andrwsv 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 28-August 11

Posted 29 August 2011 - 10:30 AM

in this line
$file = CUploadedFile::getInstance($model,'data');
data is refer to this line in the model:
array('data', 'file', 'types'=>'jpg, gif, png'),
or is the variable define in the model "public $data" ??

I try this example but don't work, do you helpme ??
0

#6 Guest_vovchikep_*

  • Group: Guests

Posted 03 September 2011 - 02:27 PM

Do you use any filter in KMPlayer?

What video renderer do both players use? Play the file and press CTRLTAB, post the report here. With code tags please. The button looks like #
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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