1

Couple of months ago,I developed a simple app using YII,one of the feature was to upload file. The feature was working well in my dev machine,couple of days ago client found the file upload feature is not working in his server since deployment.

And after that I test my dev machine that was not working too.

My controller looks:

public function actionEntry() {
        if (!Yii::app()->user->isGuest) {
            $model = new TrackForm;

            if (isset($_POST['TrackForm'])) {
                $entry = new Track;
                try {
                  $entry->product_image = $_POST['TrackForm']['product_image'];
                    $entry->product_image = CUploadedFile::getInstance($model, 'product_image');

                    if ($entry->save()) {
                        if ($entry->product_image) {
                            $entry->product_image->saveAs($entry->product_image->name, '/trackshirt/uploads');
                        }
                        }

                        $this->render('success', array('model' => $model));

                        // redirect to success page
                    }
                } catch (Exception $e) {
                    echo 'Caught exception: ', $e->getMessage(), "\n";
                }
            } else {
                $this->render('entry', array('model' => $model));
            }
        }
    }

Model is like below:

<?php
class Track extends CActiveRecord
{
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'product_details';
    }
}

My view looks:

 <?php
            $form = $this->beginWidget('CActiveForm', array(
                        'id' => 'hide-form',
                        'enableClientValidation' => true,
                        'clientOptions' => array(
                            'validateOnSubmit' => true,
                        ),
                        'htmlOptions' => array('enctype' => 'multipart/form-data'),
                    ));
            ?>

    <p class="auto-style2"><strong>Administration - Add New Product</strong></p>
    <table align="center" style="width: 650px"><td class="auto-style3" style="width: 250px">Product Image</td>
            <td>

                <?php echo $form->fileField($model, 'product_image'); ?>
            </td>
        </tr>

    </table>

    <p class="auto-style1">

                <div style="margin-leftL:-100px;">
                <?php echo CHtml::submitButton('Submit New Product Form'); ?>
            </div>

     <?php $this->endWidget(); ?>

Any idea where is the problem?I tried to debug it but every time it returns Null.

Thanks.

5
  • 2
    Does webserver have write permissions in the uploads directory? Commented Mar 24, 2012 at 11:27
  • Check your PHP and web server logs Commented Mar 24, 2012 at 11:48
  • while debugging after $entry->product_image = CUploadedFile::getInstance($model, 'product_image'); found $entry->product_image null Commented Mar 24, 2012 at 14:14
  • try doing a var_dump($_POST);die; right after if (isset($_POST['TrackForm'])) { to check what is posted Commented Mar 24, 2012 at 16:31
  • Correction. Also do try doing a var_dump($_FILES) as that is where the filename will be Commented Mar 24, 2012 at 18:11

3 Answers 3

1

this link may help you.Also dump CUploadedFile instance to check what is going on.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm surprised you don't see an error message from this:

<?php echo $form->activeFileField($model, 'product_image'); ?>

as CActiveForm doesn't have a method activeFileField() that belongs to CHtml

I think you need

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

after making this change I am able to upload a file on my server using your code with one other right brace removed here:

                         // redirect to success page
                 /*}*/
                 } catch (Exception $e) {

(probably a product of snipping when you uploaded the code)

4 Comments

oops!I am using fileField tried with activeFileField(wrong code I had posed!)
btw I am not able to upload it and CUploadedFile::getInstance($model, 'product_image'); always null
I dunno then... it works for me :( what is in $_FILES? do a var_dump on that after $model = new TrackForm; in actionEntry() to make sure your filename is being posted.
Then clearly you are not posting the filename. Maybe form type is wrong or something is wrong in the body of the form or your controller is catching a different post. You should be able to see the name of the file you chose in a var_dump of $_FILES
0

Remember that CUploadedFile::getInstance($model, $attribute) returns an object of type CUploadedFile. If you just want to make $entry->product_image the name of the image file you could do something like:

$image = CUploadedFile::getInstance($model, 'product_image');
$entry->product_image = $image->name; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.