kopecsrk
(Dzumir)
1
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,
));
}
kokomo
(Mwerlberger85)
2
$model->image = ....
// add this line
$model->your_timestamp_field_in_db = new CDbExpression('NOW()');
kopecsrk
(Dzumir)
3
Kokomo
I doesn’t want save timestamp in DB, i need save file name: “[timestamp]+task_id.jpg”
lagogz
(Lagocamanho)
4
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. 
You could also try:
$file=CUploadedFile::getInstance($model,'imageField');
$fileName='fileName';
$file->saveAs(Yii::app()->basePath.'/../images/'.$fileName);
Try typing the filename "test".
Regards.
kopecsrk
(Dzumir)
5
$timestamp has value ‘NOW()’
and now the file name is "NOW()".
lagogz
(Lagocamanho)
6
You mean after doing what I said?
ariratic
(Ari Ratic86)
7
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,
));
}
kopecsrk
(Dzumir)
8
Now file name is " NOW()bg.jpg118" but i need something that "2013-03-25[id_task].jpg"
kopecsrk
(Dzumir)
9
If i trying that , file name has name : "test".
mirunho
(D Mirecki)
10
Why not?
$fileName=date('Y-m-d') . 'name';
Maybe i miss understand something.
kopecsrk
(Dzumir)
11
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
mirunho
(D Mirecki)
12
You are using $model->file not defined before.
kopecsrk
(Dzumir)
13
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 ! 
qianggan
(qianggan@msn.com)
14
This is a good example to add current date into file name uploaded.