Duplicating Single Upload To Save With Different Models


I have a checkboxlist populated by values from a database search entry.

I am trying to upload a file and save it as separate DB entries based on the checkbox values input by the user.

Here is what I am trying to achieve.

  1. Upload a single file.

  2. Save file under different filenames based on checkbox inputs

Current result: Models are saved into DB but only one copy of the uploaded file is saved into the server.

I have tried to store copies of the uploaded file in an array to reuse it during saving of each model but this has not yielded any result as well.

Here is my code.

public function actionProcessing(){

                //Find models to populate checkboxList (no issue)

		$modelClaims = ClaimAmount::model()->with('hasAnnletter')->findAll('status!=(5||6||7)');

		$modelFiles	 = new Files;

		$number = count($modelClaims);

		for($i=0; $i<$number; $i++){

			$prf[] 			= $modelClaims[$i]->prf;

			$bp[]	 			= $modelClaims[$i]->bp;

			$combined[]		= $prf[$i].' | '.$bp[$i];


		$combined = array_combine(array_values($combined),$combined);


		//Saving Files

		$prfList = $_POST['prfList'];

		$count 	= count($prfList)*2; // Number of sets multiplied by 2 due to split below

		$prfList = implode(',',$prfList); // Split into string sets

		$prfList	= preg_split('/[,|]/',$prfList); // Split into array of PRF and BP in even and odd sets

		//Storing copies of file in array



		$uploadedFile[]				= CUploadedFile::getInstance($modelFiles,'image'); 






       $modelFiles->attributes 	= $_POST['Files'];

       $modelFiles->bp 			= $prfList[$i+1];

       $modelFiles->file_prf 		= $prfList[$i];

       $modelPrf			= Prf::model()->findbyPk($modelFiles->file_prf);

       $modelFiles->country 		= $modelPrf->bp_country;      

       $fileName 			= $prfList[$i].'_'.$uploadedFile[$i];                       

       $modelFiles->image 		= $fileName;

       $modelFiles->id_user 		= Yii::app()->user->name;

       $modelFiles->file_name           = $modelFiles->image;






            	//Save as PRFnumberFilename


		 			$modelFiles	= new Files;


         $i = $i+1;













Question is, how do I duplicate the file and save them as different filenames based on user input?

to duplicate the file upload first of all you should save the uploaded file to runtime folder and then copy that file to various folders with various names and then finally delete the image from runtime folder thats it.

Wouldn’t it work to just re-save it numerous time?

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

if($modelFiles->save()) {

   //Save as PRFnumberFilename

   foreach (xxxx as $filename)



Another question is: Why save numerous times? How many copies of the same file do you really need? Wouldn’t be better to same the “Yii::app()->basePath.’/../images/upload/’.$filename” to the different models and just reuse to file?

I know that storage is getting cheaper, but saving numerous copies of a file for website use just wrong? As a backup copy fine, but active usage???????


Jkofsky, you are actually right in that sense. I was thinking of that solution when I moved away from the computer because as you said it is cheaper and logically speaking makes more sense!

I might just be thinking too much at times that the simpler solution escapes me

Nevertheless thanks for your inputs cos it surely helps!

No Prob…

As programmers we sometimes over think it.I know I do ;D with 6 different ways to do the same thing…go figure.

One thing I always try to do is to relate what I am doing to something similar.

for example I made a Podcast site…Oh wait it’s just a blog system with an added field to store the podcast.