Uploaded images management

Hi,

I have a structure problem : i have a model named "Projets", and another one named "ImagesProjets". The latter stores image information (location, filename, alt, title) about pictures used to described a project. So, an "ImagesProjets" belongs to a "Projets".

When i create an ImagesProjets, i upload a file, get its clean filename (no accents, no double extension, this kind of stuff), and stores it on the filesystem, in a specific folder. I then store this clean filename in the "path" property of my "ImagesProjets".

When i want to create a ImagesProjets, i need to present a form with an upload field, and keep it populated if an error occurs (which is not the case for now).

When i want to modify a ImagesProjets, i’d like to see that the ImagesProjets already exists, that the associated path gets to a real file, and then show the path and the image upload (in case the user wants to modify the file).

Should i create an image object if a file exists, or something like that ? I think i’m not making it the good way for now; could someone help, please ?

TIA

My model


class ImagesProjets extends CActiveRecord {


    public $image;

    ...

    public function rules() {

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

        // will receive user inputs.

        return array(

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

            array('path, alt, title, id_projet, default, created, modified', 'required'),

            array('id_projet, default', 'numerical', 'integerOnly' => true),

            array('path, alt, title', 'length', 'max' => 250),

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

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

            array('id, path, alt, title, id_projet, default, created, modified', 'safe', 'on' => 'search'),

        );

    }



My controller




    public function actionCreate() {

        $model = new ImagesProjets;


        // Uncomment the following line if AJAX validation is needed

        // $this->performAjaxValidation($model);


        if (isset($_POST['ImagesProjets'])) {

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

            if ($model->image)

            {

                $imageName = $model->image->getName();

                $path = Utils::sanitize_filename($imageName);

                if ($model->image->saveAs(Yii::getPathOfAlias('webroot') . $this->folder . "/" . $path)) {

                    $_POST['ImagesProjets']['path'] = $path;

                    $_POST['ImagesProjets']['created'] = $_POST['ImagesProjets']['modified'] = date("Y-m-d H:i:s");

                }

            }


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

            if ($model->save()) {

                $this->redirect(array('view', 'id' => $model->id));

            }

        }


        $this->render('create', array(

            'model' => $model,

        ));

    }



Hi,

I’m tring to do the same thing, have you got some solution?

Thanks!

<moderator>

quote removed

</moderator>

Search for CUploadedFile, Menxaca.