Yii save uploaded file to base64 string

php, yii

Solution

Do it like this:

$model->attributes=$_POST['Post'];

//gives me the filename
$tmpfile = CUploadedFile::getInstance($model,'attachment');

$tmpfile_contents = file_get_contents( $tmpfile->tempName );

$model->attachment = base64_encode($tmpfile_contents);

Problem

I am trying to save an uploaded photo in my database using base64 but I can't get the data: Form: ``` <?php $form=$this->beginWidget('CActiveForm', array( 'id' => 'upload-form', 'enableAjaxValidation' => false, 'htmlOptions' => array('enctype' => 'multipart/form-data'), ));?> <?php echo $form->fileField($model, 'attachment');?> ``` Controller: ``` $model->attributes=$_POST['Post']; //gives me the filename $model->attachment=CUploadedFile::getInstance($model,'attachment'); ``` How do I get the contents so I can encode?

Original source