Yii Add Timestamp In Uploadfile

Hello,

I have problem.

I can’t pick in the upload file “name” timestamp. How i can do it?

My controller action:


public function actionCreate() {

        $model = new Task;

        $timestamp = new CDbExpression('NOW()');


        // Uncomment the following line if AJAX validation is needed

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


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

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

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

       


            if ($model->save()) {

              $model->image->saveAs(Yii::getPathOfAlias('webroot') . '/protected/files/' . $timestamp . $model->id);

             

            }

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

        }


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

            'model' => $model,

        ));

    }




$model->image = ....

// add this line

$model->your_timestamp_field_in_db = new CDbExpression('NOW()');




Kokomo

I doesn’t want save timestamp in DB, i need save file name: “[timestamp]+task_id.jpg”

Change this:




$model->image->saveAs(Yii::getPathOfAlias('webroot') . '/protected/files/' 

. $timestamp . $model->id);



for this:




$model->image->saveAs(Yii::getPathOfAlias('webroot') . '/protected/files/' 

. $timestamp, true);



Documentation for CUploadedFile:

public boolean saveAs(string $file, boolean $deleteTempFile=true)

Although it seems that the default value is true. :huh:

You could also try:

  • In actionCreate:



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

$fileName='fileName';

$file->saveAs(Yii::app()->basePath.'/../images/'.$fileName);



Try typing the filename "test".

Regards.

$timestamp has value ‘NOW()’

and now the file name is "NOW()".

You mean after doing what I said?

try this :


public function actionCreate() {

        $model = new Task;

        $timestamp = new CDbExpression('NOW()');


        // Uncomment the following line if AJAX validation is needed

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


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

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

            

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

            $fileName = "{$timestamp}{$uploadedFile}";  // $timestamp + file name

            $model->image = $fileName; 


            if ($model->save()) {

             $uploadedFile->saveAs(Yii::app()->basePath.'/../protected/files/' . $fileName . $model->id);

             

            }

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

        }


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

            'model' => $model,

        ));

    }

Now file name is " NOW()bg.jpg118" but i need something that "2013-03-25[id_task].jpg"

If i trying that , file name has name : "test".

Why not?


$fileName=date('Y-m-d') . 'name';

Maybe i miss understand something.

If i do that :


public function actionCreate() {

        $model = new Task;




        // Uncomment the following line if AJAX validation is needed

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


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

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

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

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

            $fileName=date('Y-m-d') . 'name';




            if ($model->save()) {

                $model->image->saveAs(Yii::getPathOfAlias('webroot') . '/protected/files/' . $fileName . $model->id, true);

                $model->file->saveAs(Yii::getPathOfAlias('webroot') . '/protected/files/' . $fileName . $model->id, true);

            }

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

        }


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

            'model' => $model,

        ));

    }

have error :


Fatal error: Call to a member function saveAs() on a non-object in C:\xampp\htdocs\yii_learn\protected\controllers\TaskController.php on line 76

You are using $model->file not defined before.

Sorry for that. NOW is correct :)


public function actionCreate() {

        $model = new Task;

        $timestamp = new CDbExpression('NOW()');


        // Uncomment the following line if AJAX validation is needed

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


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

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

            

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

            $fileName=date('Y-m-d') . '_ID_';  // $timestamp + file name

            $model->image = $fileName; 


            if ($model->save()) {

             $uploadedFile->saveAs(Yii::app()->basePath.'/../protected/files/' . $fileName . $model->id);

             

            }

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

        }


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

            'model' => $model,

        ));

    }

End all is done :) Thanks ! :))

This is a good example to add current date into file name uploaded.