Saving Files To A Blob Field In The Database

Hi all

I see this is a topic that has been writen about - sorry I still need help as a newbie :wink:

I have tried to follow the instructions given in the documentation but I seem to do something wrong.

I work on the following table:




CREATE TABLE IF NOT EXISTS `l_link` (

  `l_id` int(11) NOT NULL AUTO_INCREMENT,

  `l_link` varchar(100) NOT NULL,

  `l_short` varchar(15) NOT NULL,

  `l_long` varchar(45) NOT NULL,

  `l_img` blob,

  `l_sequence` int(11) NOT NULL,

  PRIMARY KEY (`l_id`),

  UNIQUE KEY `l_id_UNIQUE` (`l_id`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=72 ;



I generated model and crud for this.

To my model (LLink.php) I added




class Candidate extends CActiveRecord

{

    /**

     * Property for receiving the file from the form

     * It should be different from any other field in the database

     */

    public $l_img;

 

    public function rules()

    {

        return array(

            array('l_img', 'file', 'types'=>'jpg, gif, png'),

        );

    }

 

 

    /**

    * Saves data of the uploaded file

    */

    public function beforeSave()

    {

        if($file=CUploadedFile::getInstance($this,'l_img'))

        {

            $this->l_img=file_get_contents($file->tempName);

        }

 

    return parent::beforeSave();

    }

}



I didn’t change the controller (LLinkController.php) but changed this in view _form:




<div class="row">

	<?php echo $form->labelEx($model,'l_img'); ?>

	<?php echo $form->fileField($model,'l_img'); ?>

	<?php echo $form->error($model,'l_img'); ?>

</div>



So now when I insert or update a column the contents of my field l_img is only a couple of bytes with what seems to be the filename…

What is wrog? Is my problem that I don’t store the filename and extension?

I think the problem is that you haven’t stored extension

Thank you for you opinion! Does anyone have another idea? Please, i’m spending too much time on this problem…

I have an idea, how about NOT storing the image in the database but rather a file system path pointing to the image instead?

Thank you, I’ve thought about that too and it could be a solution! I hate not to solve the problem though because it seems to be a acceptable option… Am I too stubborn?

The advantage of storing in the db is that if I delete a record everything is cleaned up and I don’t have to go looking for files that are no longer in use.

You do not need to extension or anything else. Its work perfectly fine for me and I have almost the same configuration. I think problem could be with blob variable in database. Blob is only 65 kb so maybe your files are too big. Did you try to change it to mediumblob (16mb)?

I’m testing with files I was able to upload into the blob using myphpadmin - so this can’t be the problem.

Any idea or altenative solution?

Thanks anyway!

I have done for storing image in db. Here is code for resizing image also… Hope it is useful to you.




ob_start();


if(!empty($_FILES['Organization']['tmp_name']['logo']))

{

				

	$file = CUploadedFile::getInstance($model,'logo');

	$model->file_type = $file->type;

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

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

	fclose($fp);

	if($model->file_type == "image/png") {

	   $src_img = imagecreatefrompng($file->tempName);

           $dst_img = imagecreatetruecolor(200, 170);

           imagealphablending($dst_img, false); imagesavealpha($dst_img,true);

           $transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);

           imagefilledrectangle($dst_img, 0, 0, 200, 170, $transparent);

           imagecopyresampled($dst_img, $src_img, 0,0,0,0, 200,170, imagesx($src_img), imagesy($src_img));

           imagepng($dst_img);                                

           ob_start();

           imagepng($dst_img);

           $image_string = ob_get_contents();

           ob_end_flush();

	}

	if($model->file_type == "image/jpg" || $model->file_type == "image/jpeg") {

	   $src_img = imagecreatefromjpeg($file->tempName);

           $dst_img = imagecreatetruecolor(200, 170);

	   imagealphablending($dst_img, false);

           imagesavealpha($dst_img,true);

           $transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);

           imagefilledrectangle($dst_img, 0, 0, 200, 170, $transparent);

           imagecopyresampled($dst_img, $src_img, 0,0,0,0, 200,170, imagesx($src_img), imagesy($src_img));

           imagejpeg($dst_img);                                

           ob_start();

           imagepng($dst_img);

           $image_string = ob_get_contents();

           ob_end_flush();

	}

	if($model->file_type == "image/gif") {

	   $src_img = imagecreatefromgif($file->tempName);

           $dst_img = imagecreatetruecolor(200, 170);

	   imagealphablending($dst_img, false);

           imagesavealpha($dst_img,true);

           $transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);

           imagefilledrectangle($dst_img, 0, 0, 200, 170, $transparent);

           imagecopyresampled($dst_img, $src_img, 0,0,0,0, 200,170, imagesx($src_img), imagesy($src_img));

           imagepng($dst_img);                                

           ob_start();

           imagecreatefromgif($dst_img);

           $image_string = ob_get_contents();

           ob_end_flush();

	}

        $model->logo = $image_string;

        }

	$model->save();

}