Upload File In Blob Error (Following The Wiki)

Hi everybody,

I try to insert a file in blob in my database. Using postgresql, I was following the wiki : http://www.yiiframework.com/wiki/95/saving-files-to-a-blob-field-in-the-database/ until the moment to save my data in my database.

First I got the error : "Uploaded File cannot be blank"

I had to add “‘htmlOptions’ => array(‘enctype’ => ‘multipart/form-data’)” in my _form widget (I don’t know if it was the best solution to solve it… but this error faded)

Then I got new error : " 7 ERROR: invalid byte sequence for encoding "UTF8": 0x89. The SQL statement executed was: INSERT INTO "up_down_test" ("file_name", "file_type", "file_size", "file_content") VALUES (:yp0, :yp1, :yp2, :yp3)"

But here, I’m stuck with this error doesn’t know what to do exactly… I followed the wiki so… I found this topic issue http://code.google.com/p/yii/issues/detail?id=102 but nothing helps me.

Are there any specificities on file I want to upload or anything else ??

except changes in wiki, I changed my rules() function such as :

public function rules()


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

		// will receive user inputs.

		return array(


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

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

      array('uploadedFile', 'file', 'types'=>'jpg, gif, png','allowEmpty' => true),

			array('','safe', 'on'=>'search'),



and my _form widget is :

<?php $form=$this->beginWidget('bootstrap.widgets.BootActiveForm',array(



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

)); ?>

and my table is :

CREATE TABLE up_down_test(id serial primary key,file_name text, file_type text,file_size bigint, file_content bytea)

If you have any ideas about this error…


It looks like there is something strange in the file itself.

I have never experienced such errors, and I am saving bynary files to in the database.

Try uploading a plain text file first for be sure it works, if it works it means you are experiencing a database error, maybe you have to run set names utf-8 at connection start.

hey, thanks for reply,

I tried with a simple file BLA.txt, adding txt extension in rule and I got this warning :

htmlspecialchars() expects parameter 1 to be string, resource given


though this warning my table has been well filled with this txt file transformed in binary.

In fact it’s not in writing in database that this warning appears but when it redirects on records’ view.

It’s surely because of the binary sequence generated by the gif… it should have non UTF8 characters… hhmmm how can I manage it with postegresql…

if you have any idea…

If you’re trying to output the content to the view, you’re going to have problems. You’ll need to create a separate action to serve the content of the file with the appropriate HTTP headers. You can’t just output binary content as part of your HTML page.

the problem seemed to come to “‘charset’=>‘utf8’” in database connection properties in config/main.php, without that property it works… but I don’t know if it could be bad for other things/statement

for the view, I simply remove showing file_content

If you stop outputting the file content, you shouldn’t need to remove your ‘charset’=>‘utf8’ property.

but, removing charset was about my first problem, when I was trying to upload a file_content in blob not for viewing it afterwards

Indeed, no relation with the impossibility to view it :)