UploadedFile add attribute

[html]There are public attributes of file type:

class UploadedFile extends Object

{

public $name;


public $tempName;


public $type;


public $size;


public $error;

.

.

.

How can I add custom attribute e.g. tempUniqeName ?

The following code does not work:

if ($modelPliki->file && $modelPliki->validate()) {

$i=0;


foreach ($modelPliki->file as $file) {


	$tempUniqeName = $this->plikSciezka.$this->plikPrefix.$file->baseName . '.' . $file->extension;


	$file->saveAs($tempUniqeName);


	$modelPliki->file[$i]['tempUniqeName'] = $tempUniqeName;   //PHP Fatal Error 'yii\base\ErrorException' with message 'Cannot use object of type yii\web\UploadedFile as array'


}					

}[/html]

Please refer to the guide here http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html

At this place there is no answer to my question.

I do not have a problem with the file upload.

I want to save the file under a different name (which contains the user ID and date).

And I want the name changed to put in an additional attribute model.

So that at the end of creating new record - load file to the database (blob).

Create Active Record model for the file and fill its attributes using the data from UploadedFile instance (which should be used only to deal with the file upload anyway).

I do so of course.

But I do not know how to extend the object "file".

It has the standard attributes:

 public $ name;


 public $ tempName;


 public $ type;


 public $ size;


 public $ error;

I can not after reading/populating the $_FILES with:


$modelPliki->file = UploadedFile::getInstances($modelPliki, 'plik');

add a new attribute (eg. UniqeName).

I can do it (and I do, “workaround”), but I’d rather make it simpler.

Below the object dump $modelPliki:


<pre>

app\models\UploadForm Object

(

    [file] => Array

        (

            [0] => yii\web\UploadedFile Object

                (

                    [name] => HayekTheRoadtoStardom.txt

                    [tempName] => /tmp/phpua9K8Y

                    [type] => text/plain

                    [size] => 17738

                    [error] => 0

                    [uniqeName] => /xxxxxxxx/runtime/cache/1_2015-04-09_11:04:37_HayekTheRoadtoStardom.txt    // Here I would like to add/insert 

                )


            [1] => yii\web\UploadedFile Object

                (

                    [name] => MiltonFriedmanWolnyWybor.txt

                    [tempName] => /tmp/phpDdG7PI

                    [type] => text/plain

                    [size] => 34302

                    [error] => 0

                    [[uniqeName] => /xxxxxxxx/runtime/cache/1_2015-04-09_11:04:37_MiltonFriedmanWolnyWybor.txt  // Here I would like to add/insert

                )


        )


    [fileSingle] => 

    [uniqeName] => Array                             /*my workaround   */

        (

            [0] => /xxxxxxxx/runtime/cache/1_2015-04-09_11:04:37_HayekTheRoadtoStardom.txt

            [1] => /xxxxxxxx/runtime/cache/1_2015-04-09_11:04:37_MiltonFriedmanWolnyWybor.txt

        )

.

.

.

Do you need to do anything with the file after saving it that cannot be done in the foreach loop?

If so you don’t have to add uniqeName property to UploadedFile object.

If not - consider creating the File model for each file instance and add the unique name there so you can manipulate this model later on.

Yes I do, because of performance or ergonomics.

I created a form with a file upload widget fileinput by Kartik (Ajax uploads with drag and drop feature).

Before saving a new record, you can load (preload?) the files into remote server.

Only when saving a new record (contract), loaded on the server disk files are loaded into the database (BLOB).

To do this I have to remember the file name, size, type (actually the whole object) in the session.

The attribute [name] remember the original name, I need it too.

So can not use it.

I figured that could use an extra attribute - just [uniqeName].

I do this but as an additional attribute model. It works

But it would be more simply as an attribute of an object (type?) [file].

Below the method/function as a whole [Controller]:


public function actionCreate()

{

	$modelKontrakty = new Kontrakty();

	$modelPliki = new UploadForm();

	$UploadedFiles = new UploadedFiles();

	$this->plikSciezka = \Yii::getAlias('@runtime/cache/');

	$this->plikPrefix = \Yii::$app->user->identity->id.date('_Y-m-d_H:m:s_');

	$modelKontrakty->id_user = Yii::$app->user->id;

	$request = Yii::$app->request;

	if (Yii::$app->request->isAjax) {

		if (Yii::$app->request->isPost) {

			// $_FILES['UploadForm']['tempUniqeName']['plik'][0] = '13 tempUniqeName';

			$modelPliki->file = UploadedFile::getInstances($modelPliki, 'plik');

			if ($modelPliki->file && $modelPliki->validate()) {

				// $i=0;

				foreach ($modelPliki->file as $file) {

					// $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);

					$tempUniqeName = $this->plikSciezka.$this->plikPrefix.$file->baseName . '.' . $file->extension;

					$file->saveAs($tempUniqeName);

					// $modelPliki->uniqeName[$i++] = $tempUniqeName;

					$modelPliki->uniqeName[] = $tempUniqeName;

					// $modelPliki->file[$i]['tempUniqeName'] = $tempUniqeName;

				}

				\Yii::$app->session->set('modelPliki',$modelPliki);					

			}					

			$output = ['filename' => $modelPliki->file[0]->name];

			echo json_encode($output);

		}

	} else	{

		if ($modelKontrakty->load(Yii::$app->request->post())) {

			$modelKontrakty->created = date('Y-m-d H:m:s');

			$modelKontrakty->save();

			if (\Yii::$app->session->has('modelPliki')) {

				$modelPliki = \Yii::$app->session->get('modelPliki');

				$i = 0;

				foreach ($modelPliki->file as $pliki) {

					$UploadedFiles = new UploadedFiles();

					$UploadedFiles->id_contract = $modelKontrakty->id;

					$UploadedFiles->filename = $pliki->name;

					$UploadedFiles->filetype = $pliki->type;

					$UploadedFiles->filesize = $pliki->size;

					$UploadedFiles->fileContent = file_get_contents($modelPliki->uniqeName[$i]);

					$UploadedFiles->save();

					unlink($modelPliki->uniqeName[$i++]);

				}

				$modelPliki = \Yii::$app->session->remove('modelPliki');					

			}

		} else {

		}

		return $this->render('create', [

			'model' => $modelKontrakty,

		]);

	}

}