Image Upload (Empty File)

I am having trouble uploading an image to the db. The db table always shows [BLOB - 0 B] when a record is saved. I have tried manually uploading an image to the table, and that works. The validation seems to be working. After save() I link between profile table and image table, and that works just fine. Any insights would be very helpful.

Image Model:




['org_picture', 'image', 'extensions' => 'jpg, jpeg, gif, png', 'maxFiles' => 1, 'maxSize' => 1024 * 100, 'skipOnEmpty' => true, 'on' => 'name_desc'],


// ...


   public function handleForm2()

    {

        if ($this->validate() && $this->save()) {

            return $this;

        }

        return false;

    }



Profile Model:




    public function handleForm2($image)

    {

        if ($this->validate() && $this->save()) {

            $this->link('image', $image);

            return $this;

        }

        return False;

    }



Controller:




$profile = profile::findOne($profileId);

        if (!isset($profile)) {

            throw new NotFoundHttpException("The requested profile was not found.");

        }


$image = new image();


 if ($image->load(Yii::$app->request->Post()) && 

            $image->handleForm2() &&

            $profile->load(Yii::$app->request->Post()) && 

            $profile->handleForm2($image)) {


            return isset($_POST['continue']) ? $this->redirect(['form3', 'profileId' => $profile->id]) : $this->goHome();

 } else {

            return $this->render('form2_name_desc', ['profile' => $profile, 'image' => $image]);

 }



View:




<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>


    <?= $form->field($profile, 'org_name')->textInput(['maxlength' => true]) ?>


    <?= $form->field($profile, 'description')->textarea(['rows' => 6]) ?>


    <?= $form->field($image, 'org_picture')->fileInput() ?>


     <div class="form-group">

        <?= Html::submitButton('Save & Continue', [

        	'method' => 'POST',

        	'class' => 'btn btn-primary',

        	'name' => 'continue',

        ]) ?>

    </div>

    <div class="form-group">

		<?= HTML::submitbutton('Save & Exit', [

			'method' => 'POST',

			'class' => 'btn btn-primary',

			'name' => 'exit',

		]) ?>

    </div>


    <?php ActiveForm::end(); ?>



Storing image in db is not good, save it in separate folder and save the path name in db.

This link helps You.

if you want store the image in db, use BLOB as data type in database and save the uploaded file, not a post value of image.

Refer Here

Thanks for your reply. I read that db storage of images is fine for anything under 256KB (http://stackoverflow.com/questions/5613898/storing-images-in-sql-server). The link that you posted would work fine if I were working with the SQL database directly, but I am trying to use Yii ActiveRecord. I have followed the Yii guide for uploading files, and it seems to work fine except for the little detail about a blank file being loaded. I must be missing something.

I’m not sure what you mean by not saving a “post value of the image.” Again, thanks for your help.

EDIT: I think I see what you are referring to now. The image is not in $_POST, but in $_FILE. I think that steers me in the right direction. :)

I thought I would post my working code for anyone who is trying to upload images to the db. The guide and tutorials all seem to only cover uploading to server.




// View

 <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

 <?= $form->field($image, 'org_image')->fileInput() ?>

        // button and end form




// Model

    // Validation

['org_image', 'file', 'extensions' => 'jpg, jpeg, gif, png', 'maxFiles' => 1, 'maxSize' => 1024 * 64, 'skipOnEmpty' => true, 'on' => 'org_image'],


    // Method for upload

public function handleForm() {

        if($_FILES['Image']['size']['org_image'] > 0) {

            $fileName = $_FILES['Image']['name']['org_image'];

            $tmpName  = $_FILES['Image']['tmp_name']['org_image'];

            $fileSize = $_FILES['Image']['size']['org_image'];

            $fileType = $_FILES['Image']['type']['org_image'];


            $content = fopen($tmpName, 'r');


            if(!get_magic_quotes_gpc())

            {

                $fileName = addslashes($fileName);

            }

            

            $q = 'INSERT INTO image (org_image, org_type, org_size) VALUES (:content, :type, :size)';

            $cmd = Yii::$app->db->createCommand($q);

            $cmd->bindValue(':content', $content, PDO::PARAM_LOB);

            $cmd->bindValue(':type', $fileType, PDO::PARAM_STR);

            $cmd->bindValue(':size', $fileSize, PDO::PARAM_INT);

            $cmd->execute();

            return $this;

        } 

    }




// Controller

if ($image->load(Yii::$app->request->Post()) && $image->handleForm()) {

       //  return redirect ...

} else {

       //  return render form ...



The file upload works great. Note that I don’t have any validation working on this yet. A combination of the second link posted by ilaiya above and Larry Ullman’s Yii book (for using DAO) were a huge help.

Edit: I’m not sure why, but the validation rules just started working. I added those above.

Could you maybe post that code in the Tutorials forum too, please? :)

Done :)

Thanks Steve :)